A. 多态练习一工资系统
实现课本例9-3题目要求。根据4类不同员工类型分别进行工资的计算。
抽象基类Employee表示员工,这个类派生出SalariedEmployee、HourlyEmplyee和CommissionEmployee类,CommissionEmployee类派生出BasePlusCommissionEmployee类。(课本例9-3图)
1.固定工:每周工资一样,与工作时间长短无关,由SalariedEmployee类实现;
2.计时工:按时计酬,超过40小时算加班工资,由HourlyEmplyee类实现;
3.雇佣员工:按销售百分比例计算,由CommissionEmployee类实现;
4.底薪雇佣员工:在底薪之上增加销售百分比。在本期内,公司准备对底薪雇佣员工升薪10%,由BasePlusCommissionEmployee类实现。
在主函数中
创建4个派生类的对象,调用Earning()方法输出。
多态的使用,创建含有4个派生类元素的Employee数组,调用Earning()方法输出。
【注:】此题编译通过即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| using System;
namespace CSharp_Week7_A { abstract class Employee { public string Name { get; protected set; } public Employee(string namae) { Name = namae; } public abstract double GetSal(); public virtual void Clear() { } public void WriteName() { Console.WriteLine(Name); } public void Earning() { Console.WriteLine(GetSal()); } } class SalariedEmployee : Employee { public double Bsal { get; set; } public SalariedEmployee(string namae, double x) : base(namae) { Bsal = x; Clear(); } public override double GetSal() { return Bsal; } } class HourlyEmployee : Employee { public double SalPrh { get; protected set; } public int WorkT { get; set; } public HourlyEmployee(string namae, double preh) : base(namae) { SalPrh = preh; } public void NoteWork(int t) { WorkT += t; } public override void Clear() { WorkT = 0; } const int HourLaw = 40; public override double GetSal() { if (WorkT <= HourLaw) return SalPrh * WorkT; else return SalPrh * HourLaw + 1.5 * SalPrh * (WorkT - HourLaw); } } class CommissionEmployee : Employee { double Rate { get; set; } double ComVa { get; set; } public CommissionEmployee(string namae, double x) : base(namae) { Rate = x; } public override void Clear() { ComVa = 0; } public void NoteCom(double x) { ComVa += x; } public override double GetSal() { return ComVa * Rate; } } class BasePlusCommissionEmployee : CommissionEmployee { public double Bsal { get; set; } public BasePlusCommissionEmployee(string namae, double x, double r) : base(namae, r) { Bsal = x; } public override double GetSal() { return base.GetSal() + Bsal; } } class Program { static void Main(string[] args) { Employee[] eps = new Employee[4]; SalariedEmployee epa = new SalariedEmployee("A", 10000); HourlyEmployee epb = new HourlyEmployee("B", 250); epb.NoteWork(50); CommissionEmployee epc = new CommissionEmployee("C", 0.05); epc.NoteCom(1e7); BasePlusCommissionEmployee epd = new BasePlusCommissionEmployee("D", 10000, 0.05); epd.NoteCom(1e7); eps[0] = epa; eps[1] = epb; eps[2] = epc; eps[3] = epd; foreach (Employee ep in eps) { ep.WriteName(); ep.Earning(); } } } }
|
B. 工资系统+生日
修改例题9-3,在Employee类中添加新的字段birthDate表示员工的生日。(如:DateTime
dt=new DateTime(2000,12,21))
birthDate的数据类型为DateTime结构体类型(C#自带)。假设工资每月处理一次。
创建Employee数组,存储不同类型员工对象的引用。
可以循环输出每种类型员工的姓名和工资。如果本月是员工的生日,则工资增加100元。
【注:】本题编译通过即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| using System;
namespace CSharp_Week7_A { abstract class Employee { public string Name { get; protected set; } public DateTime Birth { get; set; } public Employee(string namae) { Name = namae; Birth = new DateTime(1900, 1, 1); } public bool Bouns() { return DateTime.Now.Month == Birth.Month; } public abstract double GetSal(); public virtual void Clear() { } public void SetBirth(int y, int m, int d) { Birth = new DateTime(y, m, d); } public void WriteName() { Console.WriteLine(Name); } public double GetTotSal() { return GetSal() + (Bouns() ? 100 : 0); } public void Earning() { Console.WriteLine(GetTotSal()); } } class SalariedEmployee : Employee { public double Bsal { get; set; } public SalariedEmployee(string namae, double x) : base(namae) { Bsal = x; Clear(); } public override double GetSal() { return Bsal; } } class HourlyEmployee : Employee { public double SalPrh { get; protected set; } public int WorkT { get; set; } public HourlyEmployee(string namae, double preh) : base(namae) { SalPrh = preh; } public void NoteWork(int t) { WorkT += t; } public override void Clear() { WorkT = 0; } const int HourLaw = 40; public override double GetSal() { if (WorkT <= HourLaw) return SalPrh * WorkT; else return SalPrh * HourLaw + 1.5 * SalPrh * (WorkT - HourLaw); } } class CommissionEmployee : Employee { double Rate { get; set; } double ComVa { get; set; } public CommissionEmployee(string namae, double x) : base(namae) { Rate = x; } public override void Clear() { ComVa = 0; } public void NoteCom(double x) { ComVa += x; } public override double GetSal() { return ComVa * Rate; } } class BasePlusCommissionEmployee : CommissionEmployee { public double Bsal { get; set; } public BasePlusCommissionEmployee(string namae, double x, double r) : base(namae, r) { Bsal = x; } public override double GetSal() { return base.GetSal() + Bsal; } } class Program { static void Main(string[] args) { Employee[] eps = new Employee[4]; SalariedEmployee epa = new SalariedEmployee("A", 10000); epa.SetBirth(1926, 8, 17); eps[0] = epa; HourlyEmployee epb = new HourlyEmployee("B", 250); epb.SetBirth(1997, 7, 1); epb.NoteWork(50); eps[1] = epb; CommissionEmployee epc = new CommissionEmployee("C", 0.05); epc.NoteCom(1e7); epc.SetBirth(2002, 8, 17); eps[2] = epc; BasePlusCommissionEmployee epd = new BasePlusCommissionEmployee("D", 10000, 0.05); epd.NoteCom(1e7); epd.SetBirth(2020, 11, 30); eps[3] = epd; foreach (Employee ep in eps) { ep.WriteName(); ep.Earning(); } } } }
|
C. 工资系统+计件工
修改例题9-3,增加Employee类的另一个派生类PieceWorker,表示计件工。
PieceWorker类具有专用的实例变量wage和pieces分别表示每件的工资和生产的件数。
在PieceWorker类中提供Earning方法的具体实现,计算员工的收入。计算方法是将件数乘以每件的工资。
创建一个Employee数组,存储新的类层次中每个具体类对象的引用,显示每个员工收入。
【注:】本题编译通过即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| using System;
namespace CSharp_Week7_C { abstract class Employee { public string Name { get; protected set; } public DateTime Birth { get; set; } public Employee(string namae) { Name = namae; Birth = new DateTime(1900, 1, 1); } public bool Bouns() { return DateTime.Now.Month == Birth.Month; } public abstract double GetSal(); public virtual void Clear() { } public void SetBirth(int y, int m, int d) { Birth = new DateTime(y, m, d); } public void WriteName() { Console.WriteLine(Name); } public double GetTotSal() { return GetSal() + (Bouns() ? 100 : 0); } public void Earning() { Console.WriteLine(GetTotSal()); } } class SalariedEmployee : Employee { public double Bsal { get; set; } public SalariedEmployee(string namae, double x) : base(namae) { Bsal = x; Clear(); } public override double GetSal() { return Bsal; } } class HourlyEmployee : Employee { public double SalPrh { get; protected set; } public int WorkT { get; set; } public HourlyEmployee(string namae, double preh) : base(namae) { SalPrh = preh; } public void NoteWork(int t) { WorkT += t; } public override void Clear() { WorkT = 0; } const int HourLaw = 40; public override double GetSal() { if (WorkT <= HourLaw) return SalPrh * WorkT; else return SalPrh * HourLaw + 1.5 * SalPrh * (WorkT - HourLaw); } } class CommissionEmployee : Employee { double Rate { get; set; } double ComVa { get; set; } public CommissionEmployee(string namae, double x) : base(namae) { Rate = x; } public override void Clear() { ComVa = 0; } public void NoteCom(double x) { ComVa += x; } public override double GetSal() { return ComVa * Rate; } } class BasePlusCommissionEmployee : CommissionEmployee { public double Bsal { get; set; } public BasePlusCommissionEmployee(string namae, double x, double r) : base(namae, r) { Bsal = x; } public override double GetSal() { return base.GetSal() + Bsal; } } class PieceWorker : Employee { double wage { get; set; } int pieces { get; set; } public PieceWorker(string namae, double x) : base(namae) { wage = x; } public override void Clear() { pieces = 0; } public void NotePieces(int x) { pieces += x; } public override double GetSal() { return pieces * wage; } } class Program { static void Main(string[] args) { Employee[] eps = new Employee[5]; SalariedEmployee epa = new SalariedEmployee("A", 10000); epa.SetBirth(1926, 8, 17); eps[0] = epa; HourlyEmployee epb = new HourlyEmployee("B", 250); epb.SetBirth(1997, 7, 1); epb.NoteWork(50); eps[1] = epb; CommissionEmployee epc = new CommissionEmployee("C", 0.05); epc.NoteCom(1e7); epc.SetBirth(2002, 8, 17); eps[2] = epc; BasePlusCommissionEmployee epd = new BasePlusCommissionEmployee("D", 10000, 0.05); epd.NoteCom(1e7); epd.SetBirth(2020, 11, 30); eps[3] = epd; PieceWorker epe = new PieceWorker("E", 100); epe.NotePieces(100); epe.SetBirth(2077, 12, 31); eps[4] = epe; foreach (Employee ep in eps) { ep.WriteName(); ep.Earning(); } } } }
|
D. 类和接口
定义一个动物类Animal,包含字段姓名、年龄,有1个带参数的构造方法,初始化2个私有字段。抽象方法叫Shout();
鸽子类派生自Animal,新增私有字段羽毛(默认白色);覆写方法Shout(),方法体输出相关信息(自己组织格式)*岁的鸽子姓名咕咕叫;
猫类派生自Animal,新增属性胡须长度;覆写方法Shout(),方法体输出相关信息(自己组织格式)*岁的小猫姓名喵喵叫;
定义一个交通工具类Vehicle,包含颜色,品牌。有1个无参数的构造方法,初始化2个私有字段(默认值自己给)。
派生类飞机继承Vehicle。新增飞行高度属性。
定义接口IFly,包含方法Fly()。鸽子类、飞机类均继承该接口,实现Fly()。鸽子类的Fly()方法,输出*颜色的鸽子飞。飞机类的Fly()方法输出*品牌的飞机飞行在*千米的高空。
在主函数中生成对象,测试类中的方法。
【注:】本题编译通过即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| using System;
namespace CSharp_Week7_D { interface IFly { void Fly(); } abstract class Animal { public string Name { get; set; } public int Age { get; set; } public Animal(string namae, int x) { Name = namae; Age = x; } public abstract void Shout(); } class Pigeon : Animal, IFly { public int Wing { get; set; } public Pigeon(string namae, int x) : base(namae, x) { Wing = 0xffffff; } public override void Shout() { Console.WriteLine("{0}岁的鸽子{1}咕咕叫", Age, Name); } public void Fly() { Console.WriteLine("{0}颜色的鸽子飞", string.Format("#{0:X}", Wing)); } } class Cat : Animal { public int Leng { get; set; } public Cat(string namae, int x) : base(namae, x) { Leng = 0; } public override void Shout() { Console.WriteLine("{0}岁的小猫{1}喵喵叫", Age, Name); } } class Vehicle { public int Color { get; set; } public string Company { get; set; } public Vehicle() { Color = 0xffffff; Company = "XJTU"; } } class Plane : Vehicle, IFly { public int Height { get; set; } public void Fly() { Console.WriteLine("{0}品牌的飞机飞行在{1}千米的高空", Company, Height); } } class Program { static void Main(string[] args) { Pigeon zdk = new Pigeon("Zeondik", 18); zdk.Shout(); zdk.Fly(); Plane fakeloc = new Plane(); fakeloc.Height = 114514; fakeloc.Fly(); } } }
|
E. *选做*——窗体程序“计算利率”
学习MOOC视频12.4例题计算利率,完成以下窗体程序。
要求:
1.熟悉Visual Studio新建Windows Form项目;
2.按照图示设计界面;
\3. 编写“确定”按钮的事件。
4.运行程序,输入“本金”、“利率”,选择“年龄”,点击“确定”按钮,“本息合计”显示计算结果。
【注】本题自愿完成。
解决方案
最后更新时间:
纰漏之处, 还望海涵, 请您联系作者, 我会尽快改正错误!