功能
编写控制台应用程序,设计一个普通职工类Employee,其工资为基本工资(1000)加上工龄工资(每年增加30元)。从Employee类派生一个本科生类UEmployee,其工资为普通职工算法的1.5倍;另从Employee类派生一个研究生类SEmployee,其工资为普通职工算法的2倍,要求计算工资用虚方法实现;分别用相关数据进行测试。
程序using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class Employee { private double bsalary = 1000; private double psalary; private int n; public int Pn { get { return n; } set { n = value; } } public virtual double ComSalary() { Console.Write("工作年数:"); Pn = int.Parse(Console.ReadLine()); psalary = bsalary + 30 * Pn; return psalary; } public class UEmployee : Employee { new public double ComSalary() { return 1.5 * base.ComSalary(); } } public class SEmployee : Employee { public override double ComSalary() { return 2 * base.ComSalary(); } } class Program { static void Main(string[] args) { Console.WriteLine("普通职工:"); Employee emp1 = new Employee(); Console.WriteLine("该普通职工的工资为:{0}", emp1.ComSalary()); Console.WriteLine("本科生职工:"); UEmployee emp2 = new UEmployee(); Console.WriteLine("本科生职工的工资为:{0}", emp2.ComSalary()); Console.WriteLine("研究生职工:"); SEmployee emp3 = new SEmployee(); Console.WriteLine("研究生职工的工资为:{0}", emp3.ComSalary()); Console.ReadKey(); } } } }运行结果
如果想了解更多物联网、智能家居项目知识,可以关注我的程序设计专栏。
或者关注公众号。
编写不易,感谢支持。