【问题标题】:How to Get Base Class Instance from a Derived Class如何从派生类中获取基类实例
【发布时间】:2013-10-15 02:26:39
【问题描述】:

我不知道这是否可能,但我正在尝试从派生类中获取基类实例。在 C# 中,我可以使用 base 关键字来访问基类的属性和方法(当然),但我想使用 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;
        }
    }
}

【问题讨论】:

  • 一个代码 sn-p 值 1000 字。请提供您想要实现的目标
  • 你能告诉我们你的代码吗,我不太明白你想要做什么,谢谢。
  • @TimSchmelter OP 知道这一点,但不清楚他想要什么......也许类似于"return base;"
  • 如果您使用派生类的实例,则没有base instance
  • 我想你的意思是这样的return base;,这给了问题?

标签: c# .net inheritance derived-class base-class


【解决方案1】:

我对他们的要求的解释与这里的其他答案略有不同,所以我想我会提供 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));
}

结果是一个用子类的基类属性填充的新对象。

【讨论】:

    【解决方案2】:
     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();
        }
    }
    

    【讨论】:

    • 无论您的代码有多好或多不好,如果没有一些清晰而详细的解释,这并不能回答问题。
    【解决方案3】:

    第 1 点:如果你想在子类中创建基类实例,那就不值得了。您已经可以在孩子中访问公共事物。

    第 2 点:如果你已经初始化了子类并且现在想要获取基类“实例”,那么如果它没有被初始化,你怎么能得到它(因为现在基类实例不存在于物理内存中,并且有只是那里的子类实例)?

    【讨论】:

      【解决方案4】:

      你没有为你的问题提供代码,但我猜你想要类似的东西

      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"
      

      【讨论】:

        【解决方案5】:

        派生实例是基础实例。它只是内存中的一个对象实例。

        示例:

        public class A : B 
        {
        }
        
        var thing = new A();
        

        thingA 的一个实例,也是B 的一个实例。
        例如,您可以这样写:
        B thing2 = thing;

        【讨论】:

          【解决方案6】:

          如果您使用派生类的实例,则没有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 的实例。

          【讨论】:

            【解决方案7】:

            问题没有得到尽可能清楚的解释。但是,通常,您最好使用抽象基类和方法,然后覆盖所需的方法。然后,您可以在这种情况下根据需要使用 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();
               }
            }
            

            }

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-07-09
              • 1970-01-01
              • 1970-01-01
              • 2013-05-20
              • 2014-09-29
              • 2013-02-12
              • 2014-04-27
              • 1970-01-01
              相关资源
              最近更新 更多