• 继承 示例1


    创建枚举类

    namespace Main
    {
        class Class1
        {
        }
    
        public enum Gender { 
          male,female
        }
        public enum Popularity { 
        
        }
    }

    创建父类

    namespace Main
    {
        public class Employee
        {
            public string  ID { get; set; }
            public int Age { get; set; }
            public string Name { get; set; }
            public Gender Gender { get; set; }
            public int Popularity { get; set; }
        }
    }

    创建SE类:程序员

    namespace Main
    {
        public class SE:Employee 
        {
             //带参构造函数,仍然可以调用抽取到Employment类中的属性
    public SE(string id,string name,int age,Gender gender,int popularity) { this.ID = id; this.Name = name; this.Age = age; this.Gender = gender; this.Popularity = popularity; } public SE() { }
    //人气值
    private int _popularity; public int Poplarity { get { return _popularity; } set { _popularity = value; } } public string SayHi() { string message = string.Format("大家好,我是{0},今年{1}年,工号是{2},我的人气值高达{3}!", this.Name, this.Age, this.ID, this.Popularity); return message; } } }

    创建PM类:项目经理

    namespace Main
    {
        public class MP:Employee
        {
             //带参构造函数,仍然可以调用抽取到Employment类中的属性
    public MP(string id,string name,int age,Gender gender,int yearOfExperience) { this.ID=id; this.Name=name; this.Age=age; this.Gender=gender; this.YearOfExperience=yearOfExperience; } public MP() { //无参构造函数
    }
    private int _yearOfExperience;
    //资历
    public int YearOfExperience { get {return _yearOfExperience; } set{_yearOfExperience =value;} } public string SayHi() { string message; message=string .Format("大家好,我是{0},今年{1},项目管理经验{2}年。",this.Name ,this.Age ,this.YearOfExperience); return message ; } } }

    在测试类Main()中调用

    namespace Main
    {
        class Program
        {
            static void Main(string[] args)
            {
                //实例化一个SE对象
    SE engineer
    = new SE("112","艾边成",25,Gender.male,100); Console.WriteLine(engineer .SayHi());
    //实例化一个MP对象
    MP pm
    = new MP("890","盖茨",50,Gender.female,30); Console.WriteLine(pm.SayHi()); Console.ReadLine(); } } }

    输出结果

  • 相关阅读:
    1.17 Python基础知识
    反射
    面向对象——类
    异常处理
    面向对象——静态方法、类方法、属性法法,其他特殊方法
    面向对象——继承
    question
    configparser模块
    hashlib模块
    shelve模块
  • 原文地址:https://www.cnblogs.com/WuXuanKun/p/5375117.html
Copyright © 2020-2023  润新知