• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
我的学习笔记
   首页       联系   管理    订阅  订阅

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());
        }
    }

运行结果.
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);
       }
}

运行结果.
no parameters function
2+6=8

posted @ 2008-09-08 18:07  吴有鋆  阅读(445)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3