大白话之继承

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

namespace inherit
{
    class Program
    {
        static void Main(string[] args)
        {
            Animal a = new zebra();
            a.move(); //只能识别老版本的方法
            //zebra b = new zebra();
            //b.move();
            Console.ReadLine();
        }
    }
    public class Animal
    {
        protected string _skincolor;
        protected int _heiht;
        protected int _weight;
        public virtual void move()
        {
            Console.WriteLine("动物的移动方法运行");
        }
    }
    public class horse : Animal
    {
        private new string _skincolor;
        public string Skincolor
        {
            get
            {
                return _skincolor;
            }
            set
            {
                this._skincolor = value;
            }
        }
        public override void move()
        {
            Console.WriteLine("白龙马可以跑动");
        }
        //public void ShowHorse()
        //{
        //    Console.WriteLine("可以展示马了{0}", this._skincolor);
        //}
    }
    public class zebra : horse
    {
        public override void move()
        {
            Console.WriteLine("斑马可以跑动");
        }
    }
}

类内的元数据会被拷贝到子类中
基类的数据会成为子类的成员 但要受到权限限制
元数据描述数据的数据 基类的代码都可以被子类用
private 的数据 也将被继承 但是不能用  静态的也不行
子类 -- 继承基类的类
支持多层继承 但不支持多重继承
子类的实例必然包含父类定义的
我是子类我优先  呵呵
如果试图子类中显示用父类的数据,要用 new 来声明是用的子类的 而不是基类的
父类引用只能用老版本 不识别新版本 Animal a=new horse() 中a是负引 不能识别horse类中new 来重新声明的字段或者方法
object的tostring()方法输出的是对应的类名 要用 public override string ToString() 来对其进行重写
引用控制着有那些成员可以被识别 而具体执行取决于那个实例 先检查方法能否调用后执行

原文地址:https://www.cnblogs.com/ivy/p/1214799.html