C#语言学习--基础部分(二十) 类的赋值,new,override,virtual

 定义override方法的注意事项

1.virtual,override不能使用在private方法上

2.重写的方法必须同名,参数列表一致,返回类型一值

3.拥有相面的访问级别

4.只能重写虚方法

5,已经定义的override方法不能再用virtual定义

6.在子类中不用overrid定义父类的同名方法,编译器报错,但是你可以用new关键字重新定义该方法(此时定义字类对像访问时此方法,时就会访问子类的该方法而不会访问父类的同名方法了)

Console Demo

Progamm.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InherienceDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //父类引用可以引用子类的对像,父关引用只能调用子类继承自父类的方法,不能引用子类所独有的方法.
            
//new关键字是子类隐藏继承的父类方法
            
//overrride是字类重写了父类中的方法
            
//父类引用可以调用子类重写父类的方法,而不是调用父类原来的方法
            Mammal newMammal = new Horse("小马");  //不能引用子类当中所独有的方法
            newMammal.GetName();
            newMammal.Breath();
            newMammal.SuckleYoung();
            newMammal.Talking(); //此处调用的是父类的talking
            
//子类引用不能直接引用父类对像,除非将父类对像的数据类型强制转换为子类,实际开发中很少将父类对像转换为子类去引用
            
//try
            
//{
            
//    Horse newHorse = (Horse)new Mammal("小小马");
            
//    newHorse.Breath();
            
//    newHorse.SuckleYoung();
            
//    newHorse.GetName();
            
//}
            
//catch (Exception ex)
            
//{
            
//    Console.WriteLine(ex.Message);
            
//}
           
            
//newHorse.Trot();

            Horse horse = new Horse("");
            horse.Breath();
            horse.SuckleYoung();
            horse.Trot();
            Whale whale = new Whale("鲸鱼");
            whale.Breath();
            whale.SuckleYoung();
            whale.Swim();
            //Mammal mammal = new Mammal();
            
//mammal.name = "哺乳动物";
            
//mammal.Breath();
            
//mammal.SuckleYoung();

            
//Horse horse = new Horse();
            
//horse.name = "马";
            
//horse.Breath();
            
//horse.SuckleYoung();
            
//horse.Trot();
           
            
//Whale whale = new Whale();
            
//whale.name = "鲸鱼";
            
//whale.Breath();
            
//whale.SuckleYoung();
            
//whale.Swim();
        }
    }
}

Mammal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InherienceDemo
{
    class Mammal
    {
        //public string name;
        private string name;
        public Mammal() { } //(若子类执行默认构造函数时必须提供)
        public  Mammal(string name)
        {
            this.name = name;
        }
        public string GetName()
        {
            return name;

        }
        public void Talking()
        {
            Console.WriteLine("Mammal talking");
        }
        public virtual void Breath() //定义为虚方法,子类当中可进行重写
        {
            Console.WriteLine(name+",正在呼吸");
        }
        public void SuckleYoung()
        {
            Console.WriteLine(name+",正在哺乳");
        }
    }
}

Horse.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InherienceDemo
{
    class Horse:Mammal
    {
        public Horse(string name)
            : base(name)
        { 
        }
       override public void Breath()
        {
            Console.WriteLine("马在呼吸");
        }
       new public void Talking()
        {
            Console.WriteLine("Horse talking");
        }
        public void Trot()
        {
           Console.WriteLine(GetName()+",正在奔跑");
        }
    }
}

Whale.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InherienceDemo
{
    class Whale:Mammal
    {
        //public void Swim()
        
//{
        
//    Console.WriteLine(name+",正在游水");
        
//}
        
//base能够显式调用父类当中的一些非私有的字段,属性和方法,若是base里不加name则是调用Mammal里的默认构造方法
        public Whale(string name):base(name)  
        {
        }
        public void Swim()
        {
            Console.WriteLine(GetName()+",正在游水");
        }
    }
}

 

posted on 2012-09-02 16:46  松波  阅读(601)  评论(0编辑  收藏  举报

导航