本文介绍了在 Java 中转换对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对在 Java 中强制转换对象意味着什么感到困惑.
I'm confused about what it means to cast objects in Java.
说你有...
Superclass variable = new Subclass object();
(Superclass variable).method();
这里发生了什么?是变量类型发生了变化,还是变量中的对象发生了变化?很迷茫.
What is happening here? Does the variable type change, or is it the object within the variable that changes? Very confused.
推荐答案
看看这个示例:
public class A {
//statements
}
public class B extends A {
public void foo() { }
}
A a=new B();
//To execute **foo()** method.
((B)a).foo();
这篇关于在 Java 中转换对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!