继承实例

using System;

using System.Collections.Generic;

using System.Linq; using System.Text;

//2014.3.13

namespace _5.继承

{

    class Program

    {

        static void Main(string[] args)

        {

            中国人 c1 = new 中国人();

            c1.Name = "JackChen";

            c1.SayHello();

            c1.户口 = "北京";

            c1.功夫();

            韩国人 k1 = new 韩国人();

            k1.Name = "金三顺";

            k1.SayHello();

            k1.饭量 = "五碗泡菜";

            k1.做泡菜();

            Person p1 = c1;//创建一个人,给了一个中国人。

            p1.SayHello();

            Person p2 = k1;//创建一个人,给了一个韩国人。

            p2.SayHello();

            中国人 zgr = (中国人)p1;//创建一个中国人,给了一个Person,这个Person是中国人。

            p1.SayHello();

            Console.ReadKey();

        }

    }

    class Person

    {

        public string Name { get; set; }

        public int Age { get; set; }

        public void SayHello()

        {

            Console.WriteLine("{0}",this.Name);

        }

    }

    class 中国人 : Person

    {

        public string 户口 { get; set; }

        public void 功夫()

        {

            Console.WriteLine("我打!");

        }

    }

    class 韩国人 : Person

    {

        public string 饭量 { get; set; }

        public void 做泡菜()

        {

            Console.WriteLine("泡菜有毒!");

        }

    }

}

原文地址:https://www.cnblogs.com/gyt-xtt/p/3639031.html