c#中"base"关键字的目的是什么?

ACP*_*ACP 44 c# base keyword

因此,对于我的应用程序的每个页面中的一些通用可重用方法的已用基类...

public class BaseClass:System.Web.UI.Page
{
   public string GetRandomPasswordUsingGUID(int length)
   {
      string guidResult = System.Guid.NewGuid().ToString();
      guidResult = guidResult.Replace("-", string.Empty);
      return guidResult.Substring(0, length);
   }
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我想使用这种方法,我会这样做,

public partial class forms_age_group : BaseClass
{
      protected void Page_Load(object sender, EventArgs e)
      {
            //i would just call it like this
            string pass = GetRandomPasswordUsingGUID(10);
      }
}
Run Code Online (Sandbox Code Playgroud)

它做了我想要的,但有一个"Base"关键字处理c#中的基类...我真的想知道什么时候应该在我的派生类中使用base关键字....

好的例子......

Mat*_*nen 65

base关键字用于链接构造时,指基类或当你想访问一个成员已被覆盖或隐藏在当前类的基类(方法,属性,任何东西).例如,

class A {
    protected virtual void Foo() {
        Console.WriteLine("I'm A");
    }
}

class B : A {
    protected override void Foo() {
        Console.WriteLine("I'm B");
    }

    public void Bar() {
        Foo();
        base.Foo();
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这些定义,

new B().Bar();
Run Code Online (Sandbox Code Playgroud)

会输出

I'm B
I'm A
Run Code Online (Sandbox Code Playgroud)

  • 它不仅限于功能.如果类(父级和子级)存在歧义,base关键字将阐明用户正在谈论的方法/数据成员. (7认同)
  • @KyleDelaney不,A.Foo()是一个静态方法调用.由于A.Foo()不是静态的,如果你这样做,你会收到错误. (2认同)

Mic*_*uen 9

您将baseoverride功能时使用关键字,但仍希望发生重写功能.

例:

 public class Car
 {
     public virtual bool DetectHit() 
     { 
         detect if car bumped
         if bumped then activate airbag 
     }
 }


 public class SmartCar : Car
 {
     public override bool DetectHit()
     {
         bool isHit = base.DetectHit();

         if (isHit) { send sms and gps location to family and rescuer }

         // so the deriver of this smart car 
         // can still get the hit detection information
         return isHit; 
     }
 }


 public sealed class SafeCar : SmartCar
 {
     public override bool DetectHit()
     {
         bool isHit = base.DetectHit();

         if (isHit) { stop the engine }

         return isHit;
     }
 }
Run Code Online (Sandbox Code Playgroud)


aba*_*hev 6

如果你在类中有相同的成员并且它是超类,那么从超类中调用成员的唯一方法是 - 使用base关键字:

protected override void OnRender(EventArgs e)
{
   // do something

   base.OnRender(e);

   // just OnRender(e); will bring a StakOverFlowException
   // because it's equal to this.OnRender(e);
}
Run Code Online (Sandbox Code Playgroud)


小智 6

c#中\xe2\x80\x9cbase\xe2\x80\x9d关键字的真正用途如下:假设你只想调用父类的参数化构造函数——那么你可以使用base并传递参数,请参阅下面的例子...

\n\n

例子 -

\n\n
 class Clsparent\n{\n    public Clsparent()\n    {\n        Console.WriteLine("This is Clsparent class constructor");\n    }\n    public Clsparent(int a, int b)\n    {\n        Console.WriteLine("a value is=" + a + " , b value is=" + b);\n    }\n}\nclass Clschild : Clsparent\n{\n    public Clschild() : base(3, 4)\n    {\n        Console.WriteLine("This is Clschild class constructor");\n    }\n}\nclass Program\n{\n    static void Main(string[] args)\n    {\n        Clschild objclschild = new Clschild();\n        Console.Read();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n