Base用法“粗”解

来源:互联网 发布:房屋平面图纸软件 编辑:程序博客网 时间:2024/04/27 21:44

base 关键字用于从派生类中访问基类的成员

  • 调用基类上已被其他方法重写的方法。
  • 指定创建派生类实例时应调用的基类构造函数。

基类访问只能在构造函数、实例方法或实例属性访问器中进行。

从静态方法中使用 base 关键字是错误的。

示例

在本例中,基类 Person 和派生类 Employee 都有一个名为 Getinfo 的方法。通过使用 base 关键字,可以从派生类中调用基类上的 Getinfo 方法。

// keywords_base.cs
// Accessing base class members
using System;
   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 {
   public static void Main() 
   {
      Employee E = new Employee();
      E.GetInfo();
   }
}

输出

Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG

有关其他示例,请参见 newvirtualoverride

示例

本示例显示如何指定在创建派生类实例时调用的基类构造函数。

// keywords_base2.cs
using System;
public class MyBase
{
   int num;
 
   public MyBase() 
   {
      Console.WriteLine("in MyBase()");
   }
 
   public MyBase(int i )
   {
      num = i;
      Console.WriteLine("in MyBase(int i)");
   }
 
   public int GetNum()
   {
      return num;
   }
}
 
public class MyDerived: MyBase
{
   // This constructor will call MyBase.MyBase()
   public MyDerived() : base()
   {
   }
 
    // This constructor will call MyBase.MyBase(int i)
   public MyDerived(int i) : base(i)
   {
   }
 
   public static void Main() 
   {
      MyDerived md = new MyDerived();
      MyDerived md1 = new MyDerived(1);
   }
}

自已写的一个小程序:

using System;

 

namespace ConsoleApplication6

{

     /// <summary>

     /// Class1 的摘要说明。

     /// </summary>

     class Test

     {

         /// <summary>

         /// 应用程序的主入口点。

         /// </summary>

         [STAThread]

         static void Main(string[] args)

         {

              //

              // TODO: 在此处添加代码以启动应用程序

              //

              Person objPerson=new Person();

              objPerson.Name="eNet";

              objPerson.Old=18;

              objPerson.FrondOf="Watch TV";

              objPerson.DisplaySelfInfo();

              Console.WriteLine("================================================");

 

              Boy objBoy=new Boy();

              objBoy.Name="eNet";

              objBoy.Old=24;

              objBoy.FrondOf="play basketball";

              objBoy.GirlFriendName="Anny";

              objBoy.DisplaySelfInfo();

         }

     }

     public class Person

     {

         protected string name="";

         protected int old=1;

         protected string frondOf="";

         public string Name

         {            

              set

              {

                   name=value;

              }

         }

         public int Old

         {   

              set

              {

                   old=value;

              }

         }

         public string FrondOf

         {       

              set

              {

                   frondOf=value;

              }

         }

         public virtual void DisplaySelfInfo()

         {

              Console.WriteLine("My name is:{0} ,and I am {1} years old now,and I am frond of {2}!",name,old,frondOf);

         }

     }

     public class Boy:Person

     {

         protected string girlFriendName="";

         public string GirlFriendName

         {

              set

              {

                   girlFriendName=value;

              }

         }

         public override void DisplaySelfInfo()

         {

              base.DisplaySelfInfo ();

              Console.WriteLine("my girlfriend's name is {0},she is very beautiful,I love her,and she love me too,we happy every day !",girlFriendName);

         }

     }

}

原创粉丝点击