执行以下代码:
ClassA myClass = new ClassA()
myClass.varA = "Teststring"
BaseClassB cls = new BaseClassB()
cls.methodA(myClass)
我有以下课程
class BaseClassA
{
}
class ClassA:BaseClass
{
string varA;
}
class ClassB:BaseClass
{
string varB;
}
然后一种方法
class BaseClassB
{
public void methodA(BaseClassA myVar)
{
// How can I display varA that is given with myClass (which is myVar)?
// It should also be able to display varB when an object of type ClassB is given
// So I have to make a downcast here but I don't know if I have to downcast to ClassA or ClassB
}
}
我知道这是主要知识,但有人可以帮助我吗?