class ClassF //基类 { //公有字段 public string name = "张三"; public string age; //公有属性 public string Age { get { return age; } set { age = value; } } //虚拟方法 public virtual double Income(double time) { double income = time * 100.0 + 2000.0; return income; } } class ClassS : ClassF //派生类 { //重写虚拟方法 public override double Income(double time) { double income = time * 100.0 + 3000.0; return income; } static void Main(string[] args) { ClassS cs = new ClassS(); Console.Write("姓名:"); Console.WriteLine(cs.name); //继承了公有字段 Console.Write("请输入工龄:"); cs.Age = Console.ReadLine(); //继承了公有属性 Console.Write("工资:"); Console.WriteLine(cs.Income(Convert.ToDouble(cs.Age)).ToString()); //继承了虚拟方法 Console.ReadKey(); } }
运行结果:
姓名:张三
请输入工龄:20
工资:5000