override和overload的区别
1、override
class Program
{
public class Employee
{
public string name;
protected decimal basepay;
public Employee(string name, decimal basepay)
{
this.name = name;
this.basepay = basepay;
}
public virtual decimal CalculatePay()
{
return basepay;
}
}
public class SalesEmployee : Employee
{
private decimal salesbonus;
public SalesEmployee(string name, decimal basepay, decimal salesbonus):base(name,basepay)//与基类通讯,调用基类的构造函数
{
this.salesbonus = salesbonus;
}
public override decimal CalculatePay()
{
return basepay + salesbonus;
}
}
static void Main(string[] args)
{Employee employee2 = new Employee("Bob", 1200);
SalesEmployee employee1 = new SalesEmployee("Alice",1000, 500);
Console.WriteLine("Employee " + employee1.name +" earned: " + employee1.CalculatePay());
Console.WriteLine("Employee " + employee2.name +" earned: " + employee2.CalculatePay());
}
}
{
public class Employee
{
public string name;
protected decimal basepay;
public Employee(string name, decimal basepay)
{
this.name = name;
this.basepay = basepay;
}
public virtual decimal CalculatePay()
{
return basepay;
}
}
public class SalesEmployee : Employee
{
private decimal salesbonus;
public SalesEmployee(string name, decimal basepay, decimal salesbonus):base(name,basepay)//与基类通讯,调用基类的构造函数
{
this.salesbonus = salesbonus;
}
public override decimal CalculatePay()
{
return basepay + salesbonus;
}
}
static void Main(string[] args)
{Employee employee2 = new Employee("Bob", 1200);
SalesEmployee employee1 = new SalesEmployee("Alice",1000, 500);
Console.WriteLine("Employee " + employee1.name +" earned: " + employee1.CalculatePay());
Console.WriteLine("Employee " + employee2.name +" earned: " + employee2.CalculatePay());
}
}
运行结果.
Employee Alice earned: 1500
Employee Bob earned: 1200
2、overload
class Class_B
{
public void a()
{
Console.WriteLine("no parameters function");
}
public void a(int i, int j)
{
Console.WriteLine("{0}+{1}={2}", i, j, i + j);
}
}
public class test
{
public static void Main(string[] args)
{
Class_B b = new Class_B();
b.a();
b.a(2, 6);
}
}
{
public void a()
{
Console.WriteLine("no parameters function");
}
public void a(int i, int j)
{
Console.WriteLine("{0}+{1}={2}", i, j, i + j);
}
}
public class test
{
public static void Main(string[] args)
{
Class_B b = new Class_B();
b.a();
b.a(2, 6);
}
}
运行结果.
no parameters function
2+6=8