请考虑以下场景:
Class Class1
Function Func() as String
End Function
End Class
Class Class2
Function Func() as String
End Function
Function Func2() as String
End Function
End Class
Class Class3
Function GetClassObject as Object
If (certain condition meets)
return new Class1();
Else
return new Class2();
End If
End Function
Main()
Object obj1 = GetClassObject();
obj1.Func(); // Error: obj1.Func() is not defined:
End Main
End Class
问题:如果由于某种原因我无法从公共接口类继承 Class1 和 Class2,如何访问 obj1.Func()?
谢谢
更新:我用来解决问题但失败的一种方法如下:
Interface ICommon
Function Func() as string
End Interface
Class Class3
...
Main()
Dim obj1 as ICommon = TryCast(GetClassObject(), ICommon); //Error: obj1 is "Nothing"
obj1.Func()
or simply:
TryCast(GetClassObject(), ICommon).Func() //Error: obj1 is Nothing
End Main
...
End Class