里氏转化(向上转型)

static void Main(string[] args)
        {
            Chinese c = new Chinese();
            c.Speak();//普通话
            c.Eat();//中餐

            ShenzhenRen s = new ShenzhenRen();
            s.Speak();//粤语
            s.Eat();//粤菜
            //向上转型:
            //ch只能访问shenzhenren继承Chinese成员
            //定义父类的类型变量存储子类的对象
            Chinese ch = new ShenzhenRen();
            //继承eat(),自定义eat(),speak()
            ch.Eat();//中餐,优先调用继承的
            ch.Speak();//粤语,继承被覆盖就调用子类重写的;

        }

猜你喜欢

转载自blog.csdn.net/qq_36561650/article/details/80924552