在Java中转换对象 [英] Casting objects in Java

查看:107
本文介绍了在Java中转换对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设你有...

 超类变量= new Sublclass object(); 
(超类变量).method();

这里发生了什么?变量类型是否改变,还是变量中的对象改变?非常困惑。

解决方案

请看看这个例子:

  public class A {
// statements
}

public class B extends A {
public void foo(){}
}

A a = new B();

//执行** foo()**方法。

((B)a).foo();


I'm confused about what it means to cast objects in Java.

Say you have...

Superclass variable = new Sublclass 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.

解决方案

Have a look at this sample:

public class A {
  //statements
}

public class B extends A {
  public void foo() { }
}

A a=new B();

//To execute **foo()** method.

((B)a).foo();

这篇关于在Java中转换对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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