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();
}
}
}
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();
}
}
}
相关文章推荐
- 分享通用基类库-C#重写微软的SQLHELP的数据库操作基类
- c#隐藏基类方法和重写基类方法的区别
- C#继承类重写与隐藏基类方法的区别
- c#隐藏基类方法和重写基类方法的区别
- (原创)c#学习笔记10--定义类成员02--类成员的其他议题02--调用重写或隐藏的基类方法
- C# 基类 派生类 方法隐藏 方法重写
- 分享通用基类库-C#重写微软的SQLHELP的数据库操作基类
- 关于C#中,继承类重写与隐藏基类方法的区别!
- 关于在C#中对类中的隐藏基类方法和重写方法的理解
- C#继承类重写与隐藏基类方法的区别
- C#继承类重写与隐藏基类方法的区别
- c#隐藏和重写基类方法的区别
- 学习C#高级编程--在重写基类的方法中,如何调用基类的函数
- C#基础:多态:基类可以定义并实现虚(virtual)方法,派生类可以重写(override)这些方法
- [导入]C#继承类重写与隐藏基类方法的区别
- c#隐藏和重写基类方法的异同
- c#隐藏和重写基类方法的区别
- [C#]把文件分割又重写了一遍
- 根据C#代码重写了datagridcomboxcolumn,发现这个还是挺好的
- C#访问数据库的基类