Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java泛型类参数类型推断_Java_Generics_Type Inference - Fatal编程技术网

Java泛型类参数类型推断

Java泛型类参数类型推断,java,generics,type-inference,Java,Generics,Type Inference,鉴于接口: public interface BasedOnOther<T, U extends BasedList<T>> { public T getOther(); public void staticStatisfied(final U list); } 其中Y扩展了BasedList 相反: class X implements BasedOnOther<SomeType, Y> { public SomeType g

鉴于接口:

public interface BasedOnOther<T, U extends BasedList<T>> {

    public T getOther();

    public void staticStatisfied(final U list);

}
其中
Y扩展了BasedList

相反:

class X implements BasedOnOther<SomeType, Y> {
    public SomeType getOther() { ... }
    public void staticStatisfied(final Y list) { ... }
}
类X实现BasedOnOther{ 公共SomeType getOther(){…} 公共void statistisfied(最终Y列表){…} } 其中
Y扩展了BasedList

更新:科林建议

public interface BasedOnOther<T> {
    public T getOther();
    public void staticSatisfied(BasedList<T> list);
}
基于其他的公共接口{
公共T getOther();
公共资源(基本列表);
}
不可能创建以下实现:

public class X implements BasedOnOther<SomeType> {
    public SomeType getOther() { ... }
    public void staticStatisfied(MemoryModel list);
} // Does not compile, as it does not implement the interface.
公共类X实现BasedOnOther{
公共SomeType getOther(){…}
公共无效静态统计(MemoryModel列表);
}//未编译,因为它未实现接口。

其中,
MemoryModel扩展BasedList
,这是必需的(因为它提供了其他方法)。

如果您实际上不需要类型参数
U extends BasedList
,如果您实际上不需要在类中执行任何需要
BasedList
的特定子类/实现的操作,那么它看起来就不需要类型参数
U extends BasedList
。接口可以是:

public interface BasedOnOther<T> {
  public T getOther();
  public void staticSatisfied(BasedList<T> list);
}
不过,这似乎是一种浪费,我并不认为最初的声明看起来那么糟糕。

这个呢

public interface BasedOnOther<T> {
    public T getOther();
    public <U extends BasedList<T>> void staticStatisfied(final U list);
}
基于其他的公共接口{
公共T getOther();
公共无效统计(最终U列表);
}

科林几乎是正确的。您可能想要的是:

public interface BasedOnOther<T> {
  public T getOther();
  public void staticSatisfied(BasedList<? extends T> list);
}
基于其他的公共接口{
公共T getOther();

我最近遇到了一个非常类似的问题

我建议,如果您不需要特别提及
MemoryModel
,即如果
U extends BasedList
足够,那么我肯定会按照Pepe的回答去做

但是,如果您必须对至少两个方法进行类型检查,这两个方法都必须使用
MemoryModel
,并且Pepe的答案中的类型推断是不够的,那么使笨拙/冗长的参数化构造函数的使用更简单的唯一方法是利用泛型方法参数推断您需要为每个构造函数创建通用的静态工厂方法,工厂方法进行类型推断(在Java中,构造函数不能进行类型推断)

如何做到这一点已在本节中介绍

有效Java,Joshua Block著;第27项:偏爱通用方法


我也解释了这一点,并引用了解决方案(带有代码)。

谢谢您的时间。我也认为这是不可能的。我将使用丑陋的方法。
public interface BasedOnOther<T> {
    public T getOther();
    public <U extends BasedList<T>> void staticStatisfied(final U list);
}
public interface BasedOnOther<T> {
  public T getOther();
  public void staticSatisfied(BasedList<? extends T> list);
}
public test() {
  Number n1;
  Integer n2; //Integer extends Number.  Integer is a more-specific type of Number.

  n1 = n2; //no problem
  n2 = n1; //Type mismatch because the cast might fail.

  List<Number> l1;
  List<Integer> l2;
  List<? extends Number> l3;

  l1 = l3; //No problem.
  l1 = l2; //Type mismatch because the cast might fail.
}