• 继承 示例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(); } } }

    输出结果

  • 相关阅读:
    C#和sqlserver中生成新的32位GUID
    IIS7下swfupload上传大文件出现404错误
    jQuery 判断是否为数字的方法 及 转换数字函数
    js数组与字符串的相互转换方法
    jquery 中如何将数组转化为json字符串,然后再转化回来?
    Firemonkey Android 虚拟机
    Eclipse apk 签名
    win10 修改hosts
    eclipse 预览Android界面报错
    夜神模拟器
  • 原文地址:https://www.cnblogs.com/WuXuanKun/p/5375117.html
Copyright © 2020-2023  润新知