C#面向对象基础--继承


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

namespace 继续
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string language { get; set; }

        public virtual void SayHello()
        {
            Console.WriteLine("我的名字是:{0},年龄是{1},语言是{2}",Name,Age,language);
        }
    }

    //声明中国人类,继承Person类
    public class Chinese:Person
    {
        public override void SayHello()
        {
            base.SayHello();
        }
    }

    //声明美国人类,继承Person类
    public class USA:Person
    {
        //重写Person类的SayHello方法
        public override void SayHello()
        {
            Console.WriteLine("My name is:{0},Age is: {1},language is:{2}", Name, Age,language);
        }
    }

    //声明日本人类,继承Person类
    public class Japanese : Person
    {
        //重写Person类的SayHello方法
        public override void SayHello()
        {
            Console.WriteLine("わはしの名前は:{0},歳は:{1},言葉は:{2}", Name, Age,language);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个中国人
            Chinese c = new Chinese();
            c.Name = "张三";
            c.Age = 20;
            c.language = "汉语";
            c.SayHello();

            //实例化一个美国人
            USA a = new USA();
            a.Name = "Tom";
            a.Age = 20;
            a.language = "EngLish";
            a.SayHello();

            //实例化一个日本人
            Japanese j = new Japanese();
            j.Name = "田中";
            j.Age = 30;
            j.language = "日本語";
            j.SayHello();

        }
    }
}

 

 
posted @ 2012-11-13 16:07  菜鸟程序猿  阅读(181)  评论(0编辑  收藏  举报