C# -- 继承规则

例子1--C#继承的常见问题:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Animal animal = new Animal();
            animal.Introduce();
            Console.WriteLine(animal.word);

            //父类指向子类 是可以的               
            Animal cat = new Cat();
            cat.Introduce();
            Console.WriteLine(cat.word);

            //子类指向父类才是不能的 所以下面代码将会报错 不能说 “父亲像儿子”
            //Cat cat2 = new Animal();
            //cat2.Introduce();
            //Console.WriteLine(cat2.word);

            //Dog类中没有重写父类的虚方法 所以调用的是父类的Introduce方法
            Animal dog = new Dog();
            dog.Introduce();
            Console.WriteLine(dog.word);

            //子类和父类存在的同名方法 但是子类的方法没用Override重写   此时调用的是子类的方法
            Bird bird = new Bird();
            bird.Introduce();
            Console.WriteLine(bird.word);

            //子类和父类存在的同名方法 如果new出来的是父类指向子类 而且子类没有重写父类的虚方法 则调用的是父类的同名方法
            Animal bird2 = new Bird();
            bird2.Introduce();
            Console.WriteLine(bird2.word);
            
        }
    }
    public class Animal
    {
        public string word;
        public virtual void Introduce()
        {
            word = "动物";
        }
    }

    public class Cat : Animal
    {
        public override void Introduce()
        {
            //重写后继续使用父类的Inroduce方法
            //base.Introduce();
            //子类自己的
            word = "";
        }
    }

    public class Pig : Animal
    {
        public override void Introduce()
        {
            word = "";
        }
    }

    public class Dog : Animal
    { 
       
    }

    public class Bird : Animal
    {
        //这里没有重写父类的Introduce方法 而是Bird类自己的
        public void Introduce()
        {
            word = "";
        }
    }
}

 结果:

例子2--子类父类字段与方法的优先级:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //优先级
            Father son = new Son();
            //如果是父类指向子类 “儿子像父亲”所以优先调用父类的同名变量type 故输出“初代”
            Console.WriteLine(son.type);
            //强制转换为Son 此时输出的是“第二代”
            Console.WriteLine(((Son)son).type);
            //当调用的是方法时 输出的是子类重写的方法
            Console.WriteLine(son.SayHi());
            //子类和父类存在的同名方法 如果new出来的是父类指向子类 而且子类没有Override重写父类的虚方法 则调用的是父类的同名方法 
            //上例子1中的 Animal bird2
        }
    }
    public class Father
    {
        public string type = "初代";

        public  virtual string SayHi()
        {
            return "大家好!我是初代.";
        }
    }

    public class Son:Father
    {
        public string type = "第二代";

        public override string SayHi()
        {
            return "大家好!我是第二代.";
        }
    }
}

结果:

结论:

子类指向子类

——则优先调用子类自生的所有成员 

父类指向子类

——则优先使用父类的所有成员字段(若要调用则强制转换)

——子类重写父类的成员方法则优先调用子类的

——若子类没有重写父类的成员方法而保持同名 则优先调用父类的同名方法 

原文地址:https://www.cnblogs.com/keiling/p/3642054.html