如何从派生类获取基类实例
我不知道这是否可行,但我试图从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
的实例,它inheritance了A的所有属性和非重写行为,但它不包含B
的实例,它包含对B
实例的(隐藏但自动)引用。 您可以将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();
thing
是A
的实例,也是B
的实例。
你可以写一下这一行:
B thing2 = thing;
第1点:如果要在子类中创建基类实例而不是它不值得。 你已经有孩子可以访问的公共事物。
第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(); } }
我解释他们的问题有点不同于其他答案,所以我想我会提供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)); }
结果是使用子类的基类属性填充的新对象。