簡體   English   中英

如何從派生類獲取基類實例

[英]How to Get Base Class Instance from a Derived Class

我不知道這是否可行,但我試圖從Derived Class中獲取Base Class實例。 在C#中,我可以使用base關鍵字來訪問Base Class的屬性和方法(當然),但我想使用base本身。 嘗試這樣做會導致“使用關鍵字'base'在此上下文中無效”錯誤。

示例代碼

public class SuperParent
{
    public int SPID;

    public SuperParent()
    {
    }
}

public class SubChild : SuperParent
{
    public SubChild(int pSPID)
    {
        base.SPID = pSPID;
    }

    public int BaseSPID
    {
        get
        {
            SuperParent sp = base;
            return sp.SPID;
        }
    }
}

如果您正在使用派生類的實例,則沒有base instance 一個例子:

class A
{
    public void Foo() { ... }
}

class B : A
{
    public void Bar() { ... }
}

B內不可能的事情:

public void Bar()
{
    // Use of keyword base not valid in this context
    var baseOfThis = base; 
}

你可以這樣做:

public void Bar()
{
    base.Foo();
}

你可以添加另一種方法

public A GetBase()
{
    return (A)this;
}

然后就可以了

public void Bar()
{ 
    var baseOfThis = GetBase();
    // equal to:
    baseOfThis = (A)this;
}

所以這個GetBase()方法可能就是你想要的。

點睛之筆是:如果你有一個實例B ,它繼承了所有屬性和非被覆蓋的行為A ,但它不包括實例的B持有的(隱藏,但自動)參考實例A 您可以將B實例轉換為A ,但它仍然是B的實例。

那么你沒有提供你的問題的代碼,但我想你想要的東西

class Base
{
    public virtual void Foo()
    {
        Console.WriteLine("base");
    }
}

class Derived : Base
{
    public override void Foo()
    {
        Console.WriteLine("derived");
    }

    //// bad
    //public Base MyBase
    //{
    //    get
    //    {
    //        return base; // Use of keyword 'base' is not valid in this context
    //    }
    //}

    // work but...
    public Base MyBase
    {
        get
        {
            return (Base)this;
        }
    }
}

但請記住, MyBase實際上是Derived類型

new Derived().MyBase.Foo(); // output "derived"

問題沒有得到盡可能清楚的解釋。 但是,通常,您最好使用抽象基類和方法,然后覆蓋所需的方法。 然后,您可以根據需要使用base.method(否則您將剛剛啟動派生類的實例)。

public abstract class foo {

   public virtual void bar(){..}
}

public class footwo : foo {
   public override void bar(){
       // do somethng else OR:
       return base.bar();
   }
}

}

派生實例是基本實例。 它只是內存中的一個對象實例。

例:

public class A : B 
{
}

var thing = new A();

thingA的實例,也是B的實例。
你可以寫一下這一行:
B thing2 = thing;

第1點:如果要在子類中創建基類實例而不是它不值得。 你已經有孩子可以訪問的公共事物。

第2點:如果你已經初始化了子類,現在想要獲得基類“實例”,那么如果它沒有被初始化你怎么能得到它(因為現在基類實例不存在於物理內存中,並且只有子類實例那里)?

我解釋他們的問題有點不同於其他答案,所以我想我會提供0.02美元。

// Create a "Parent" class that has some attributes.
public class Parent
{
    public string attribute_one { get; set; }
    public string attribute_two { get; set; }
    public string attribute_three { get; set; }
}

// Define a class called "Child" that inherits the
// attributes of the "Parent" class.
public class Child : Parent
{
    public string attribute_four { get; set; }
    public string attribute_five { get; set; }
    public string attribute_six { get; set; }
}

// Create a new instance of the "Child" class with
// all attributes of the base and derived classes.
Child child = new Child {
    attribute_one = "interesting";
    attribute_two = "strings";
    attribute_three = "to";
    attribute_four = "put";
    attribute_five = "all";
    attribute_six = "together";
};

// Create an instance of the base class that we will
// populate with the derived class attributes.
Parent parent = new Parent();

// Using reflection we are able to get the attributes
// of the base class from the existing derived class.
foreach(PropertyInfo property in child.GetType().BaseType.GetProperties())
{
    // Set the values in the base class using the ones
    // that were set in the derived class above.
    property.SetValue(parent, property.GetValue(child));
}

結果是一個填充了子類的基類屬性的新對象。

 class Parent
 {
      private Parent _parent;
      public Parent()
      {
         _parent = this;
      }

     protected Parent GetParent()
     {
         return _parent;
     }
 }

class Child : Parent
{
    private Parent _parent;
    public Child()
    {
        _parent = base.GetParent();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM