Java将一个对象转换为未实现的接口 [英] Java casting an object to an interface which is not implemented

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

问题描述

我在研究书中发现了以下问题,有点困惑:



给定以下代码,如果用于替换 * INSERT CODE
HERE * /
将启用 Roamable 类型的引用变量来引用
的对象电话课? (选择1个选项。)

  interface Roamable {} 
class Phone {}
class Tablet extended Phone implements Roamable {
// INSERT CODE HERE
}

选项包括: / p>


  1. Roamable var = new Phone();

  2. Roamable var =(Roamable)Phone();

  3. 电话();

  4. 因为介面 Roamable 和类 c $ c>不相关, Roamable 类型的引用变量
    不能引用 Phone $ c>。

我认为正确的选项是4,但是它是3。



但是, Phone 不实现 Roamable 界面,因此您无法投射,你可以吗?

解决方案

正确的答案是 3 只有电话正在投放到 Roamable 电话不是最终的,所以它认为被转换的对象,尽管被称为 Phone 可以是 Phone 实施 Roamable ,因此不会发出编译时错误或警告。



根据 JLS第5章 p>

5.5.1。参考类型铸造


给定编译时引用类型S(源)和编译时引用类型T ),如果由于以下规则没有发生编译时错误,则从S到T存在转换转换。
如果T是接口类型:



如果S不是最终类(§8.1.1),则如果存在T的超类型X ,以及S的超类型Y,使得X和Y都是可区别的参数化类型,并且X和Y的擦除是相同的,则会发生编译时错误。



否则,转换在编译时总是合法的(因为即使S不实现T,S的子类也可能)。



如果S是final class(§8.1.1),那么S必须实现T,否则会出现编译时错误。







以下代码编译:

  interface Roamable {} 
class Phone {}
class Tablet extensions电话实现Roamable {
Roamable var =(Roamable)new Phone(); //编译
}


I found following question in my study book and a bit confused:

Given the following code, which option, if used to replace /* INSERT CODE HERE */, will enable a reference variable of type Roamable to refer to an object of the Phone class? (Select 1 option.)

interface Roamable{}
class Phone {}
class Tablet extends Phone implements Roamable {
    //INSERT CODE HERE
}

Options are:

  1. Roamable var = new Phone();
  2. Roamable var = (Roamable)Phone();
  3. Roamable var = (Roamable)new Phone();
  4. Because interface Roamable and class Phone are unrelated, a reference variable of type Roamable can’t refer to an object of class Phone.

I thought the correct option is 4, however it says it is 3.

But, Phone doesn't implement Roamable interface, so you can't cast, can you?

解决方案

The correct answer is 3: The compiler sees only that a Phone is being cast to a Roamable and that Phone is not final, so it thinks that the object being cast, although referred to as Phone may be a subclass of Phone that does implement Roamable, so no compile-time error or warning is issued.

According to JLS chapter 5

5.5.1. Reference Type Casting

Given a compile-time reference type S (source) and a compile-time reference type T (target), a casting conversion exists from S to T if no compile-time errors occur due to the following rules. If T is an interface type:

If S is not a final class (§8.1.1), then, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs.

Otherwise, the cast is always legal at compile time (because even if S does not implement T, a subclass of S might).

If S is a final class (§8.1.1), then S must implement T, or a compile-time error occurs.


The following code compiles:

interface Roamable{}
class Phone {}
class Tablet extends Phone implements Roamable {
    Roamable var = (Roamable)new Phone(); // Compiles
}

这篇关于Java将一个对象转换为未实现的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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