“的真正目的是什么?”;“基础”;c#中的关键字?

“的真正目的是什么?”;“基础”;c#中的关键字?,c#,keyword,base,C#,Keyword,Base,因此,对于我的应用程序的每个页面中一些常用的可重用方法所使用的基类 public class BaseClass:System.Web.UI.Page { public string GetRandomPasswordUsingGUID(int length) { string guidResult = System.Guid.NewGuid().ToString(); guidResult = guidResult.Replace("-", string.E

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

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);
   }
}
所以如果我想用这个方法,我会

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);
      }
}
它符合我的要求,但有一个“Base”关键字处理c#中的基类。。。 我真的想知道什么时候应该在派生类中使用base关键字


任何一个好的例子…

当链接构造函数时,或者当您想要访问基类中已在当前类中重写或隐藏的成员(方法、属性、任何内容)时,
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();
    }
}
根据这些定义

new B().Bar();
将输出

I'm B
I'm A

当您重写派生类中的方法,但只想在原始功能的基础上添加其他功能时,将使用Base

例如:

  // Calling the Area base method:
  public override void Foo() 
  {
     base.Foo(); //Executes the code in the base class

     RunAdditionalProcess(); //Executes additional code
  }
public class Foo
{
    public virtual void Baz()
    {
        Console.WriteLine("Foo.Baz");
    }
}

public class Bar : Foo
{
    public override void Baz()
    {
        Console.WriteLine("Bar.Baz");
    }

    public override void Test()
    {
        base.Baz();
        Baz();
    }
}

如果类及其基类中有相同的成员,则调用基类成员的唯一方法是使用
base
关键字:

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

   base.OnRender(e);

   // just OnRender(e); will cause StakOverFlowException
   // because it's equal to this.OnRender(e);
}

base
关键字用于访问基类中已被子类中的成员覆盖(或隐藏)的成员

例如:

  // Calling the Area base method:
  public override void Foo() 
  {
     base.Foo(); //Executes the code in the base class

     RunAdditionalProcess(); //Executes additional code
  }
public class Foo
{
    public virtual void Baz()
    {
        Console.WriteLine("Foo.Baz");
    }
}

public class Bar : Foo
{
    public override void Baz()
    {
        Console.WriteLine("Bar.Baz");
    }

    public override void Test()
    {
        base.Baz();
        Baz();
    }
}
调用
Bar.Test
将输出:

Foo.Baz;
Bar.Baz;

当您
覆盖
一项功能,但仍希望覆盖的功能同时出现时,您将使用
base
关键字

例如:

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

可以使用base来填充对象基类的构造函数中的值

例如:

public class Class1
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime Birthday { get; set; }

    public Class1(int id, string name, DateTime birthday)
    {
        ID = id;
        Name = name;
        Birthday = birthday;
    }
}

public class Class2 : Class1
{
    public string Building { get; set; }
    public int SpotNumber { get; set; }
    public Class2(string building, int spotNumber, int id, 
        string name, DateTime birthday) : base(id, name, birthday)
    {
        Building = building;
        SpotNumber = spotNumber;
    }
}

public class Class3
{
    public Class3()
    {
        Class2 c = new Class2("Main", 2, 1090, "Mike Jones", DateTime.Today);
    }
}

c#中“base”关键字的真正用途如下:假设您只想调用父类的参数化构造函数,那么您可以使用base并传递参数,请参见下面的示例

范例-

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

通常,我们使用基类重用基类的子类中的属性或方法,因此不需要在子类中重复相同的属性和方法

现在,我们使用base关键字直接从基类调用构造函数或方法

范例

public override void ParentMethod() 
  {
     base.ParentMethod(); //call the parent method

     //Next code.
  }
2) 范例


Base有两种用法

  • 以功能为基础
  • 以变量为基础
  • 带功能的基础

    当base与函数一起使用时,它的目的是在父类被子类继承时使用参数调用父类。我将用一个例子来解释这一点

  • 在以下示例中,控制台打印
  • 参数为1, 这是子构造函数

  • 现在,如果删除了base(参数),则console将打印
  • 这是父构造函数, 这是子构造函数

    当您从子类实例化一个对象时,构造函数一经实例化就被调用。当您从子类继承父类时,父类和子类中的构造函数都会被调用,因为它们都是实例化的。使用base()时,直接调用父类中的构造函数。因此,如果它说base(),它意味着父类中的构造函数没有任何参数,当使用base(parameter)时,它意味着父类中的构造函数有一个参数。这是一种函数重载。在base()括号内使用的参数变量的类型由与base一起使用的函数的参数列表定义(在下面的实例中,它是子(int参数))

    演示


    带变量的基。

  • 在以下示例中,console打印
  • 父母,孩子

    在下面的示例中,如果使用base关键字,则意味着您要向子类继承的父类寻址。如果您使用它,那么您将地址指向类本身,这意味着您实例化子类时的子类,从而调用其构造函数。因此,当您使用base.value时,意味着您引用父类中的变量;当您引用this.value时,意味着您引用子类中的变量。当两者具有相同的名称时,您可以区分使用此基、此关键字引用的变量。请记住,不能在函数外的类中使用base、this关键字。您必须在函数内部使用它们来引用在全局级别初始化的变量。此外,您不能使用它们来引用在函数内部初始化的局部变量

    using System;
    
    class Parent
    {
        public string value = "Parent";
    }
    
    class Child : Parent
    {
        public string value = "Child";
    
        public Child() {
          Console.WriteLine(base.value);
          Console.WriteLine(this.value);
        }
    
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Child childObject = new Child();
        }
    }
    
    演示

    它不仅限于功能。当类(父类和子类)中存在歧义时,base关键字会澄清用户所谈论的方法/数据成员。
    A.Foo()
    是否会与
    base.Foo()
    做相同的事情?@KyleDelaney No,A.Foo()是一个静态方法调用。由于A.Foo()不是静态的,如果这样做,您将得到一个错误在重写的方法中调用它。+1表示if(isHit){删除i,它具有完全不同的含义};)@andrewWinn:这可能是C语言设计者无法将VB.NET的IsNot关键字引入C语言的主要原因。它将有一个booger的外观:-D嘿嘿