简体   繁体   English

如何访问抽象父类的实例成员?

[英]How to access instance members of an abstract parent class?

如果无法创建父类(抽象)的实例,我们如何访问子类中抽象父类的实例成员?

TL:RD; TL:RD; the answer would be, yes. 答案是肯定的。 But they are not copied. 但是它们不会被复制。

Let's say: 比方说:

public abstract class A
{
    public string Code { get;set; }
}

Is your super (parent/base) class and 是您的超级(父/母)类,并且

public class B : A
{
    public string Title { get;set; }
}

Is your derived (sub) class. 是您的派生(子)类。 The instantiation of class B would contain both the property Code as well as Title. 类B的实例将包含属性Code和Title。

Class B extends class A. This makes things possible like casting an instantiation of class B to class A by simply doing: B类扩展了A类。这使得通过简单地将B类的实例强制转换为A类成为可能。

B foo = new B();
A bar = foo as A;

Though I would advise you to use an interface rather than a superclass in order to check if an instance has a certain property you are looking for. 尽管我建议您使用接口而不是超类来检查实例是否具有您要查找的特定属性。

So again, class A properties and methods are not copied to class B. Class B extends its methods and properties to class A. 同样,A类的属性和方法不会复制到B类。B类将其方法和属性扩展到A类。

-- Edit -- -编辑-

To answer the question: both constructors are invoked, so aren't there really 2 different objects? 要回答这个问题:两个构造函数都被调用了,那么实际上不是2个不同的对象吗?

On compiler level, yes, the abstract class and the derived class are 2 different objects. 在编译器级别,是的,抽象类和派生类是2个不同的对象。 But you as a user of the instance B don't need to worry that B is actually an object consisting of B and A. 但是,作为实例B的用户,您不必担心B实际上是由B和A组成的对象。

In the above example, if you cast B down to A. And modify the property 'Code'. 在上面的示例中,如果将B强制转换为A。然后修改属性“代码”。 Then cast object back to B. The property 'Code' is still set to the new value. 然后将对象转换回B。“代码”属性仍设置为新值。

Example: 例:

B foo = new B();
foo.Code = "testfoo";
Console.WriteLine(foo.Code); //output will be testfoo;

A bar = foo as A;
bar.Code = "testbar";
Console.WriteLine(bar.Code); //output will be testbar;

But also: 但是也:

Console.WriteLine(foo.Code); //output will be testbar;

This is because the derived class B, does not actually contain a property Code. 这是因为派生类B实际上不包含属性Code。 It only says that it extends A, and A has the property Code. 它只说它扩展了A,并且A具有属性Code。

BUT! 但! Your doubt would be justified if you use new(shadows in vb.net). 如果使用new(vb.net中的阴影),您的疑问将是合理的。

Example: 例:

public abstract class A
{
    public string Code { get;set; }
}

public class B : A
{
    public new string Code { get;set; }
    public string Title { get;set; }
}

What then happens is what I think you are worried about happening. 然后发生的就是我认为您担心发生的事情。

Now both the derived and superclass have a property 'Code'. 现在,派生类和超类都具有属性“ Code”。

In this example, the output would actually differ based upon how you would look at the object. 在此示例中,输出实际上将根据您如何看待对象而有所不同。

Something really weird would happen in this case and is prone to faults if used incorrectly. 在这种情况下,确实会发生一些奇怪的事情,如果使用不当,很容易出错。

Example: 例:

B foo = new B();
foo.Code = "testfoo";
Console.WriteLine(foo.Code); //output will be testfoo;

A bar = foo as A;
bar.Code = "testbar";
Console.WriteLine(bar.Code); //output will be testbar;

But now : 但现在 :

Console.WriteLine(foo.Code); //output will be testfoo;

Also, the term Abstract does not do more for your class apart from stating that it cannot be constructed on its own, it has to be inherited in order to be constructed. 同样,术语抽象对您的类没有更多的作用,除了指出不能单独构造它,还必须继承它才能构造它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM