java 返回值不同_java – 在实现通用方法时不同的返回值类型

本文探讨了Java中关于返回值类型的通用方法覆盖问题。当子类方法覆盖父类的泛型方法时,如果省略类型参数,会导致未选中的转换警告。这是因为编译器使用继承方法的擦除,使得不同泛型方法的返回类型变得不兼容。正确做法是在子类方法声明中保留类型参数,以避免类型安全问题。此外,非泛型子类方法可以覆盖泛型超类方法,但可能产生未检查警告,因为返回类型需要通过不受检查的转换才能兼容。
摘要由CSDN通过智能技术生成

虽然第一个示例编译,它会给出一个未选中的转换警告:

// Type safety: The return type List> for foo() from the type C needs

// unchecked conversion to conform to List

public List> foo()

{

return null;

}

这里发生的是,通过声明类型参数,A.foo()和B.foo()是generic methods.然后,覆盖的C.foo()省略该类型参数。这类似于使用raw type,基本上“选择退出”该方法签名的通用类型检查。这导致编译器使用继承的方法erasures:List< String> foo()和List< Integer> foo()都成为List foo(),因此可以通过C.foo()来实现。

您可以看到,通过在C.foo()声明中保留type参数,将会出现预期的编译器错误:

// The return type is incompatible with A.foo()

public List> foo()

{

return null;

}

同样,如果任何一个接口方法没有声明一个类型参数,那么从覆盖中省略一个类型参数将无法“选择退出”该方法的通用类型检查,返回类型List保持不兼容。

The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. This is important so that library designers may freely generify methods independently of clients that define subclasses or subinterfaces of the library.

Now, let us explore an example where non-generic subtype methods

override generic supertype methods. Non-generic subtype methods are

considered overriding versions of the generic supertype methods if the

signatures’ erasures are identical.

Example (of non-generic subtype methods overriding generic supertype

methods):

06002

06003

Here the subtype methods have signatures, namely set(Object) and get()

, that are identical to the erasures of the supertype methods. These

type-erased signatures are considered override-equivalent.

There is one blemish in the case of the get method: we receive an

unchecked warning because the return types are not really compatible.

The return type of the subtype method get is Object , the return type

of the supertype method get is an unbounded type parameter. The

subtype method’s return type is neither identical to the supertype

method’s return type nor is it a subtype thereof; in both situations

the compiler would happily accept the return types as compatible.

Instead, the subtype method’s return type Object is convertible to the supertype method’s return type by means of an unchecked conversion. An unchecked warning indicates that a type check is necessary that neither the compiler nor the virtual machine can perform. In other words, the unchecked operation is not type-safe. In case of the convertible return types someone would have to make sure that the subtype method’s return value is type-compatible to the supertype method’s return type, but nobody except the programmer can ensure this.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值