第六章 继承和多态 上机1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyOffice { public class Employee { public Employee() { Console.WriteLine("我是Employee无参构造函数"); } public Employee(string id, string name, int age, Gender g) { Console.WriteLine("我是Employee带参构造函数"); this.ID = id; this.Name = name; this.Age = age; this.Gender = g; } /// <summary> /// 工号 /// </summary> protected string ID { get; set; }//属性 /// <summary> /// 年龄 /// </summary> protected int Age { get; set; } /// <summary> /// 姓名 /// protected 当前类和子类可以访问 /// </summary> protected string Name { get; set; } /// <summary> /// 性别 /// </summary> protected Gender Gender { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyOffice { /// <summary> /// 项目经理类 /// </summary> class PM:Employee { //参数的类型\个数\顺序要一致 public PM(string n, string i, int a, Gender g, int year) :base(i,n,a,g) { Console.WriteLine("我是PM带参构造函数"); ////父类的公共属性 //this.Name = name; //this.ID = id; //this.Age = age; //this.Gender = g; //自己特有的属性 this.YearOfExperience = year; } /// <summary> /// 资历 /// </summary> public int YearOfExperience { get; set; } /// <summary> /// 问好 /// </summary> /// <returns>问好的内容</returns> public string SayHi() { string message; message = string.Format( "大家好,我是 {0} ,今年 {1} 岁,项目管理经验 {2}年。", base.Name, base.Age, this.YearOfExperience ); return message; } /// <summary> /// 项目经理评分 /// </summary> /// <param name="se"></param> public void Judge(SE se, String assess, int score) { se.Assess = assess; se.Score = score; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyOffice { /// <summary> /// 程序员类 /// </summary> public class SE:Employee { //base() 隐式调用父类的无参构造函数 public SE(string id,string name,int age,Gender g,int pop) :base(id,name,age,g)//显式调用父类带参构造函数 { Console.WriteLine("我是SE带参构造函数"); ////父类的公共属性 //this.ID = id; //this.Name = name; //this.Age = age; //this.Gender = g; //自己特有属性 this.Popularity = pop; } /// <summary> /// 人气值 /// </summary> private int _popularity = 0; public int Popularity { get { return _popularity; } set { _popularity = value; } } /// <summary> /// 经理年度评分 /// </summary> private int _score = 0; public int Score { get { return _score; } set { _score = value; } } /// <summary> /// 经理评价 /// </summary> private String _assess = "未评价"; public String Assess { get { return _assess; } set { _assess = value; } } public string SayHi() { string message = string.Format("大家好,我是 {0}, 今年 {1}岁,工号是 {2},我的人气值高达 {3}!", base.Name,base.Age,base.ID,this.Popularity); return message; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyOffice { class CEO:Employee { public CEO():base()//调用父类的无参构造函数 { } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyOffice { /// <summary> /// 性别枚举 /// </summary> public enum Gender { male, female } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyOffice { class Program { static void Main(string[] args) { //程序员对象 SE se = new SE("111", "张三", 20, Gender.male, 99); Console.WriteLine(se.SayHi()); PM pm = new PM("张经理", "112", 35, Gender.male, 10); Console.WriteLine(pm.SayHi()); Console.ReadLine(); } } }