构造函数及其重载and继承

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //person p1 = new person();
            //person p2 = new person("小红");
            //person p3 = new person("小芳", 18);

            //Console.WriteLine("名字是{0},年龄为{1}",p1.Name,p1.Age);
            //Console.WriteLine("名字是{0},年龄为{1}",p2.Name,p2.Age);
            //Console.WriteLine("名字是{0},年龄为{1}",p3.Name,p3.Age);
            //构造函数的重载 能够更好的封装类 不用访问其内部成员 开始访问时就直接给它参数  不用知道函数将如何对参数进行处理
    
            中国人 c1 = new 中国人();     //构造函数是不能被其他子类所继承的
            c1.Name = "李小龙";
            c1.Sayhello();
            c1.户口 = "西安";
            c1.中国功夫();
            韩国人 h1 = new 韩国人();     //子类可以继承其父类的一般属性
            h1.Name = "金三顺";
            h1.棒子();
            h1.做泡菜();

            person p1 = c1;
            p1.Sayhello();

            中国人 c2 =(中国人) p1;  //可加类型转换 使其人类转换为中国人类  
            c2.Sayhello();


            Console.ReadKey();
            
           

        }
    }
    class person:Object  //所以类全部继承于Object类
    {   
        public string Name{set;get;}
        public int Age{set;get;}

        public void Sayhello()
        {
            Console.WriteLine("我的名字叫{0}",this.Name);
        }
        public person()
        {
            Name="未命名";
            Age=0;
        }
        public person(string name)
        {
            Name=name;
        }
        public person(string name,int age)
        {
            Name=name;
            Age=age;
        }
    }
    class 中国人:person
    {
        public string 户口
        { get; set; }
        public void 中国功夫()
        {
            Console.WriteLine("我打!!!");
        }

    }
    
    
    class 韩国人:person
    {
        public void 做泡菜()
        {
            Console.WriteLine("泡 泡菜");
        }
        public void 棒子()
        {
            Console.WriteLine("西游记是我们写的");
        }
    }
}

发布了31 篇原创文章 · 获赞 9 · 访问量 14万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章