【发布时间】:2013-07-24 08:34:04
【问题描述】:
我正在尝试这样做:
class A {
//attibutes
}
class B : A {
public int classBAttribute = 5;
// other attributes and methods
}
我的问题是,如果我有一个 A 类的实例,我如何获取 B 类的实例或访问他的属性?
B b = new B();
Application.Add(b);
//other form
A a = Application.GetA();
B b = getBFromA(a);// ??? Note: B b = a as B; does't work i tried
【问题讨论】:
-
好吧。您不能将 A 向上转换为 B。很简单。
-
另外,为了将来参考 - 您所指的属性称为类成员。 .NET 语言中的属性是完全不同的东西。 =)
-
好吧,我不知道正确的概念,我更像是一个算法专家:)
-
这就是为什么我要指出它以供未来参考。我不是在责备。 =)
标签: c# oop inheritance polymorphism