public class Employee { ////工号 //public string ID { get; set;} ////年龄 //public int Age { get; set;} ////姓名 //public string Name { get; set;} ////性别 //public Gemder Gender { get; set; } ////------------------------------------------------------实列3------------------------------------------------- ////工号 //protected string ID { get; set;} ////年龄 //protected int Age { get; set;} ////姓名 //protected string Name { get; set;} ////性别 //protected Gemder Gender { get; set; }
//-------------------------------------------实列4子类的构造函数过程--------------------------------------------------
public Employee() {
Console.WriteLine("父类无参对象构造函数"); } public Employee(string id,int age,string name,Gemder gender) { this.ID = id; this.Age = age; this.Name = name; this.Gender = gender;
}
//工号 protected string ID { get; set; } //年龄 protected int Age { get; set; } //姓名 protected string Name { get; set; } //性别 protected Gemder Gender { get; set; }
//---------------------------------------------------------------实例8------------------------------------------------------------------- //修改Employee类的Sayhi()方法 public virtual string SayHi() {
string message = string.Format("大家好"); return message;
}
-------------------------------------------------------------------------------------------------------------
public enum Gemder {
男, 女
}
----------------------------------------------------------------------------------------------------------
namespace Myoffice { public class Pm:Employee { //带参构造函数,仍然可以调用到Employee类中的属性 public Pm(string id, string name, int age, Gemder gender, int yearofExperience) { this.ID = id; this.Name = name; this.Age = age; this.Gender = gender; this.YearofExperience = yearofExperience;
} public Pm() { } //资历 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;
//}
//--------------------------------------实列2用base调用父窗体的属性---------------------------------------------------------------------------
//// 问好的内容 //public string SayHi() //{ // string message; // message = string.Format("大家好,我是{0},今年{1}岁,项目管理经验{2}年",base.Name,base.Age,this.YearofExperience); // return message;
//}
//-----------------------------------------------------------实例8--------------------------------------------------------- //修改SayHi方法 public override string SayHi() {
string message = string.Format("大家好,我是{0},今年{1}岁,项目管理经验{2}年",base.Name,base.Age,this.YearofExperience); return message;
}
}
}
namespace Myoffice { public class SE:Employee { // //带参构造函数,仍然可以调用到Employee类中的属性 //public SE(string id,string name,int age,Gemder gender,int popularity) //{ // this.ID = id; // this.Name = name; // this.Age = age; // this.Gender = gender; // this.Poppularity = popularity;
//} //public SE() //{ //} ////人气值 //private int _popularity; //public int Poppularity //{ // get { return _popularity;} // set { _popularity = value;}
//}
//问好,返回值;问好的内容 //public string SayHi() //{
// string message; // message = string.Format("大家好,我是{0},今年{1}岁,人气值是{2}", this.Name, this.Age, this.Poppularity); // return message;
//}
//--------------------------------------------------------实例4--------------------------------------------------------------------------- // //带参的构造函数 // public SE(string id, string name, int age, Gemder gender, int popularity) // { // this.ID = id; // this.Name = name; // this.Age = age; // this.Gender = gender; // this.Poppularity = popularity; // }
////无参的构造函数 // public SE() // { // }
// //人气值 // private int _popularity; // public int Poppularity // { // get { return _popularity; } // set { _popularity = value; }
// } // public string SayHi() // { // string message = string.Format("大家好,我是{0},今年{1}岁了,工号是{2},我的人气值高达{3}",base.Name,base.Age,base.ID,this.Poppularity); // return message;
// }
//--------------------------------------------------------实例5--------------------------------------------------------------------------- public SE(string id, string name,int age,Gemder gender,int popularity): base(id,age,name,gender) { //继承自父类的属性 //调用父类的构造函数可以替换掉的代码 this.ID = id; this.Age = age; this.Name = name; this.Gender = gender; //SE类扩展的属性 this.Poppularity = popularity;
}
//人气值 private int _popularity; public int Poppularity { get { return _popularity; } set { _popularity = value; }
} //问好,返回值;问好的内容
//public string SayHi() //{
// string message; // message = string.Format("大家好,我是{0},今年{1}岁,人气值是{2}", this.Name, this.Age, this.Poppularity); // return message;
//} //------------------------------------------------------实例8---------------------------------------------------------------
//修改SayHi方法 public override string SayHi() { string message = string.Format("大家好,我是{0},今年{1}岁,工号是{2},我的人气值是{3}",base.Name,base.Age,base.ID,this.Poppularity); return message;
}
}
}
-------------------------------------------------------------------------------------------------------------
public class SmallTruck:Truck { //构造函数 public SmallTruck(string type, string place):base(type,place) { this.Type = type; this.Place = place;
} public void SmallTruckRun() {
Console.WriteLine("小型车在行驶");
}
}
}
---------------------------------------------------------------------------------------------------------------
namespace Myoffice { public class Truck:Vehicle {
public Truck(string type,string place):base(type,place) {
} //TruckRun()方法 public void TruckRun() {
Console.WriteLine(string.Format("型号为{0},产地为{1}的卡车在行驶", this.Type, this.Place));
}
}
}
public class Vehicle { //型号 public string Type { set; get;} //产地 public string Place { set; get;} //构造函数 public Vehicle(string type, string place) { this.Type = type; this.Place = place; } //VehicleRun()方法
public void VehicleRun() {
Console.WriteLine("汽车在行驶");
}
}
}
-----------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
class Program {
//测试类Main()方法
static void Main(string[] args) { ////实列化一个SE对象 //SE engineer = new SE("112","艾边城",25,Gemder.男,100); //Console.WriteLine(engineer.SayHi()); //Pm pm = new Pm("890","盖茨",50,Gemder.男,30); //Console.WriteLine(pm.SayHi()); //Console.Read();
// ---------------------------------实例4-------------------------------------------------------------------- ////实例化一个SE对象 //SE engineer = new SE("112","艾边城",25,Gemder.男,100); //Console.WriteLine(engineer.SayHi()); //Console.Read();
// ---------------------------------实例6--------------------------------------------------------------------
//Truck truck = new Truck("欧迪A6","德国"); //truck.VehicleRun(); ////truck.TruckRun(); ////Console.ReadLine();
//SmallTruck smalltruck = new SmallTruck("欧迪A6","德国"); //smalltruck.VehicleRun(); //smalltruck.TruckRun(); //smalltruck.SmallTruckRun();
//--------------------------------实例8------------------------------------------------------------- SE ai = new SE("112","艾边城",25,Gemder.男,100); SE joe = new SE("113","joe",30,Gemder.女,200); //实例化P民对象 Pm pm = new Pm("890", "盖茨", 50, Gemder.男, 30); List<Employee> empls = new List<Employee>(); empls.Add(ai); empls.Add(joe); empls.Add(pm);
//遍历问好 //foreach(Employee empl in empls) //{
// if(empl is SE) // { // Console.WriteLine(((SE)empl).SayHi());
// } // if(empl is Pm) // { // Console.WriteLine(((Pm)empl).SayHi()); // }
//} foreach (Employee empl in empls) {
//不需要判断,直接调用SayHi()方法 Console.WriteLine(empl.SayHi());
} Employee ema = new SE("210","ema",33,Gemder.男,100); Console.WriteLine(ema.SayHi()); Console.Read();
}
}
}