您的位置:首页 > 编程语言 > C#

C#中基类的重写

2015-04-07 08:14 148 查看
基类中需要重写的方法和属性设置为virtual,而在继承类中将相应的属性或方法设置为override。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chap4_3
{
class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public double Salary { get; set; }

public Employee(string name, int age, double salary)
{
this.Name = name;
this.Age = age;
this.Salary = salary;
}

public virtual void Disp()
{
Console.WriteLine("{0},{1},{2}",this.Name,this.Age,this.Salary);
}
}

class Manager : Employee
{
public double Bonus { get; set; }
public Manager(string name, int age, double salary, double bonus):base(name,age,salary)
{
this.Bonus = bonus;
}

public override void Disp()
{
Console.WriteLine("{0},{1},{2}", this.Name, this.Age, this.Salary+this.Bonus);
}
}
class Program
{
static void Main(string[] args)
{
Manager mng = new Manager("zxf", 38, 4000, 1000);
mng.Disp();
Console.ReadLine();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: