在Java中调用子类的方法

新手上路,请多包涵

如果我有一个基类 Base thing = null; 其中有一个子类 class Subclass extends Base 并且我将其作为 thing = new Subclass 调用方法,我将如何具体调用一个—但不在基地?前任。 Base has only method() Subclass has method() and specialMethod() the method specialMethod() is the一个我想打电话。

原文由 eyecreate 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 297
1 个回答

如果你知道 thing 包含一个 Subclass ,你可以这样做:

 ((Subclass) thing).specialMethod()

原文由 Eric Eijkelenboom 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题