继承不能调用子类方法 [英] Inheritance can't call child class method

查看:115
本文介绍了继承不能调用子类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试调用子类方法(ChildClass扩展了SuperClass())

Hey i'm trying to call child class method(ChildClass extends SuperClass())

SuperClass s=new ChildClass();
s.childClassMethod();

它没有看到ChildClass方法,我只能调用的方法是SuperClass() 我知道这可能是一个愚蠢的问题,但无论如何都会欢呼

It doesn't see the ChildClass method the only methods i can call are from SuperClass() i know it's propably a stupid question but anyway cheers

推荐答案

是的,您看不到它,因为s是类型SuperClass的类型,没有此方法-这显然会破坏多态原则.

That's right, you can't see it because s is type SuperClass which doesn't have this method - this would obviously break Polymorphism principle.

因此,您要么必须更改((ChildClass) s).childClassMethod();之类的代码,要么将s设置为ChildClass类型.

So you either have to change the code like ((ChildClass) s).childClassMethod(); or make s as ChildClass type.

这篇关于继承不能调用子类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆