54

したがって、私のアプリケーションのすべてのページで、いくつかの一般的な再利用可能なメソッドに使用される基本クラスについて...

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

それは私が望むことをしますが、c#の基本クラスを扱う「Base」キーワードがあります...私は本当に私の派生クラスでいつ基本キーワードを使うべきか知りたいです...。

良い例...

4

9 に答える 9

77

この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
于 2010-04-15T10:08:33.507 に答える
14

base機能を使用するときにキーワードを使用しますoverrideが、オーバーライドされた機能も発生させます。

例:

 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;
     }
 }
于 2010-04-15T10:16:38.517 に答える
8

クラスとその基本クラスに同じメンバーがある場合、基本クラスのメンバーを呼び出す唯一の方法は、usingbaseキーワードです。

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);
}
于 2010-04-15T10:10:03.000 に答える
5

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();
    }
}
于 2018-09-27T08:19:52.137 に答える
4

キーワードは、サブクラスのメンバーによってオーバーライド(または非表示)された基本クラスのbaseメンバーにアクセスするために使用されます。

例えば:

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;
于 2010-04-15T10:13:32.453 に答える
2

ベースは2つの方法で使用されます。

  1. 機能を備えたベース
  2. 変数のあるベース。

機能を備えたベース

baseを関数で使用する場合、その目的は、親クラスが子クラスに継承されるときに、パラメーターを使用して親クラスを呼び出すことです。例を挙げて説明します。

  1. 次の例では、コンソールが印刷されます。

パラメータは1です。これは子コンストラクタです

  1. ここで、base(parameter)を削除すると、コンソールが印刷されます。

これは親コンストラクターです、これは子コンストラクターです

子クラスからオブジェクトをインスタンス化すると、インスタンス化されるとすぐにコンストラクターが呼び出されます。親クラスを子クラスから継承すると、親クラスと子クラスの両方がインスタンス化されるため、両方のコンストラクターが呼び出されます。base()を使用する場合は、親クラスのコンストラクターを直接呼び出します。つまり、base()とは、パラメーターのない親クラスのコンストラクターを意味し、base(parameter)を使用する場合は、パラメーターのある親クラスのコンストラクターを意味します。これは一種の関数のオーバーロードです。base()括弧内で使用されるパラメーター変数のタイプは、baseで使用される関数のパラメーターリストによって定義されます(次の例では、それはchild(intパラメーター)です)

using System;

class Parent
{
    public Parent()
    {
        Console.WriteLine("This is Parent Constructor");
    }
    public Parent(int parameter)
    {
        Console.WriteLine("parameter is " + parameter);
    }
}

class Child : Parent
{
    public Child(int parameter): base(parameter)
    {
        Console.WriteLine("This is child constructor");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Child childObject = new Child(1);
    }
}

デモ https://repl.it/@donqq/baseKeyword#main.cs


変数のあるベース。

  1. 次の例では、コンソールが印刷されます。

親、子。

次の例では、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();
    }
}

デモ https://repl.it/@donqq/Base-Class

于 2020-08-13T01:13:09.363 に答える
1

Baseは、派生クラスのメソッドをオーバーライドするが、元の機能に加えて機能を追加したい場合に使用されます

例えば:

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

     RunAdditionalProcess(); //Executes additional code
  }
于 2010-04-15T10:09:42.930 に答える
0

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);
    }
}
于 2010-04-15T13:31:32.410 に答える
0

通常、基本クラスを使用して、基本クラスの子クラスのプロパティまたはメソッドを再利用しているため、子クラスで同じプロパティとメソッドを再度繰り返す必要はありません。

ここで、baseキーワードを使用して、基本クラスからコンストラクターまたはメソッドを直接呼び出します。

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

     //Next code.
  }

2)例

class child: parent
{
    public child() : base(3, 4) //if you have parameterised constructor in base class
    {

    }
}
于 2018-09-27T09:28:09.807 に答える