Java通用接口vs通用方法,以及何时使用它 [英] Java Generic Interface vs Generic Methods, and when to use one

查看:157
本文介绍了Java通用接口vs通用方法,以及何时使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,除了语法上的差异之外,什么时候使用泛型接口来接受一个泛型参数?

  public interface Flight< T> {
void fly(T obj);
}

超过

  public interface Flight {
void< T>飞(T obj);


解决方案

em> method ,你总是让 caller 决定哪些类型参数用于类型参数。该方法的实现必须能够处理所有可能的类型参数(甚至没有办法请求实际的类型参数)。



说,像< T> void fly(T obj); 声明调用者可以为 T 使用任何类型,而实现可以依赖的唯一东西是实际类型如果< T extends Object> 已被声明)。

因此,在这个特定的例子中,它与声明 void fly(Object obj) ; ,它也允许任意对象。



相比之下, code> 是合约的一部分,可以通过接口实现指定或限制:

  public interface Flight< T> {
void fly(T obj);



$ b $ p
$ b

允许像

  public class X实现了Flight< String> {
public void fly(String obj){
}
}

修复实现端的 T 类型。或者

  public class NumberFlight< N extends Number>实现Flight< N> {
public void fly(N obj){
}
}



<






<$ c的签名当接口本身是另一个方法签名的一部分时,$ c> interface 也很重要,例如

  public void foo(Flight< ;? super String> f){
f.fly(some string value);
}

这里, Flight 实现,您传递给 foo 的实现必须能够使用 String 值,所以 Flight< String> Flight< CharSequence> Flight<> ,但不是 Flight< Integer> 。声明这样一个合约需要在接口上的类型参数,而不是在接口的方法中。


I was wondering, aside from syntactic difference, when would one use a generic interface over a method that accepts a generic parameter?

public interface Flight<T>{
   void fly(T obj);
}

over

public interface Flight{
    void <T> fly(T obj);
}

解决方案

If you declare a generic method, you always let the caller decide, which type arguments to use for the type parameters. The implementation of the method must be able to deal with all possible types arguments (and it doesn’t even have a way to ask for the actual type arguments).

That said, a method like <T> void fly(T obj); states that the caller may use any type for T while the only thing the implementation can rely on is that the actual type for T will be assignable to Object (like if <T extends Object> had been declared).

So in this specific example, it’s not different to the declaration void fly(Object obj);, which also allows arbitrary objects.

In contrast, a type parameter on an interface is part of the contract and may be specified or restricted by an implementation of the interface:

public interface Flight<T>{
   void fly(T obj);
}

allows implementations like

public class X implements Flight<String> {
   public void fly(String obj) {
   }
}

fixing the type of T on the implementation side. Or

public class NumberFlight<N extends Number> implements Flight<N> {
   public void fly(N obj) {
   }
}

being still generic but restricting the type.


The signature of an interface is also important when the interface itself is a part of another method signature, e.g.

public void foo(Flight<? super String> f) {
    f.fly("some string value");
}

here, the Flight implementation, which you pass to foo, must be capable of consuming a String value, so Flight<String> or Flight<CharSequence> or Flight<Object> are sufficient, but not Flight<Integer>. Declaring such a contract requires type parameters on the interface, not at the interface’s methods.

这篇关于Java通用接口vs通用方法,以及何时使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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