当前位置:编程学习 > C#/ASP.NET >>

C#中base两种用法

1、调用基类中的重名方法
[csharp] 
public class Person 
   { 
       protected string ssn = "444-55-6666"; 
       protected string name = "John L. Malgraine"; 
 
       public virtual void GetInfo() 
       { 
           Console.WriteLine("Name: {0}", name); 
           Console.WriteLine("SSN: {0}", ssn); 
       } 
   } 
   class Employee : Person 
   { 
       public string id = "ABC567EFG"; 
       public override void GetInfo() 
       { 
           // Calling the base class GetInfo method: 
           base.GetInfo(); 
           Console.WriteLine("Employee ID: {0}", id); 
       } 
   } 
 
   class TestClass 
   { 
       static void Main() 
       { 
           Employee E = new Employee(); 
           E.GetInfo(); 
       } 
   } 
   /*
   Output
   Name: John L. Malgraine
   SSN: 444-55-6666
   Employee ID: ABC567EFG
   */ 

 
2、调用基类的构造方法
[csharp] 
ublic class ActionCancelEventArgs : System.ComponentModel.CancelEventArgs 

public ActionCancelEventArgs() : this(false) {} 
public ActionCancelEventArgs(bool cancel) : this(false, String.Empty) {} 
public ActionCancelEventArgs(bool cancel, string message) : base(cancel) 

this.message = message; 

public string Message{ get; set;} 
}  

作者:luolunz
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,