首页 > 代码库 > 父类不能转换成子类
父类不能转换成子类
父类不能转换成子类
Exception in thread "main" java.lang.ClassCastException: Person cannot be cast to Boy at Test.main(Test.java:5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
public class Test { public static void main(String[] args) { Person person = new Person(); Boy boy = (Boy) person; boy.eat(); }}class Person { public void eat() { System.out.println("The people were eating"); }}class Boy extends Person { public void eat() { System.out.println("The boy were eating"); }}
父类不能转换成子类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。