类的继承

C#类的继承,一个冒号即可。子类:父类

namespace ConsoleApplication1
{
    class Parent //父类
    {
        public int Height { get; set; } //身高
        public string SkinColor { get; set; } //肤色
    }
    class Sun:Parent //继承
    {
        public void Show()
        {
            Console.WriteLine("儿子身高{0}cm,肤色{1}", Height, SkinColor);//直接使用父类的属性
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sun s = new Sun(); //创建子类的对象
            s.Height = 180; 
            s.SkinColor = "黄色";
            s.Show(); //子类的方法
        }
    }
}

 

posted @ 2019-04-29 20:27  夕西行  阅读(162)  评论(0编辑  收藏  举报