gpt4 book ai didi

泛型类中的 Java 泛型方法

转载 作者:IT老高 更新时间:2023-10-28 21:09:10 25 4
gpt4 key购买 nike

如果在Java中创建一个泛型类(该类有泛型类型参数),可以使用泛型方法(该方法接受泛型类型参数)吗?

考虑以下示例:

public class MyClass {
public <K> K doSomething(K k){
return k;
}
}

public class MyGenericClass<T> {
public <K> K doSomething(K k){
return k;
}

public <K> List<K> makeSingletonList(K k){
return Collections.singletonList(k);
}
}

正如您对泛型方法所期望的那样,我可以调用 doSomething(K)MyClass 的实例上与任何对象:

MyClass clazz = new MyClass();
String string = clazz.doSomething("String");
Integer integer = clazz.doSomething(1);

但是,如果我尝试使用 MyGenericClass 的实例没有指定泛型,我调用 doSomething(K)返回 Object ,不管是什么K传入:

MyGenericClass untyped = new MyGenericClass();
// this doesn't compile - "Incompatible types. Required: String, Found: Object"
String string = untyped.doSomething("String");

奇怪的是,如果返回类型是泛型类,它将编译 - 例如。 List<K> (实际上,这可以解释 - 请参阅下面的答案):

MyGenericClass untyped = new MyGenericClass();
List<String> list = untyped.makeSingletonList("String"); // this compiles

此外,如果泛型类被键入,即使只使用通配符,它​​也会编译:

MyGenericClass<?> wildcard = new MyGenericClass();
String string = wildcard.doSomething("String"); // this compiles
  • 为什么在非类型化泛型类中调用泛型方法不起作用有充分的理由吗?

  • 我是否缺少一些与泛型类和泛型方法相关的巧妙技巧?

编辑:

为了澄清,我希望无类型或原始类型的泛型类不尊重泛型类的类型参数(因为它们尚未提供)。但是,我不清楚为什么无类型或原始类型的泛型类意味着泛型方法不被尊重。

事实证明,这个问题已经在 SO 上提出,c.f. this question .对此的答案解释说,当一个类是无类型的/以其原始形式时,所有泛型从该类中删除 - 包括泛型方法的类型。

但是,对于为什么会这样,并没有真正的解释。所以请允许我澄清一下我的问题:

  • 为什么 Java 会删除无类型或原始类型泛型类上的泛型方法类型?这有充分的理由吗,还是只是疏忽?

编辑 - JLS 的讨论:

已建议(在回答上一个 SO 问题和此问题时)在 JLS 4.8 中处理此问题,其中指出:

The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.

我很清楚这与非类型化类有何关系——类泛型类型被删除类型替换。如果类泛型已绑定(bind),则删除类型对应于这些边界。如果它们未绑定(bind),则删除类型为 Object - 例如

// unbound class types
public class MyGenericClass<T> {
public T doSomething(T t) { return t; }
}
MyGenericClass untyped = new MyGenericClass();
Object t = untyped.doSomething("String");

// bound class types
public class MyBoundedGenericClass<T extends Number> {
public T doSomething(T t) { return t; }
}
MyBoundedGenericClass bounded = new MyBoundedGenericClass();
Object t1 = bounded.doSomething("String"); // does not compile
Number t2 = bounded.doSomething(1); // does compile

虽然泛型方法是实例方法,但我不清楚 JLS 4.8 是否适用于泛型方法。泛型方法的类型(在前面的示例中为 <K>)不是无类型的,因为它的类型由方法参数确定 - 只有类是无类型/原始类型的。

最佳答案

'为了向后兼容'似乎是删除类泛型类型的充分理由——它是必需的,例如允许您返回一个无类型列表并将其传递给一些遗留代码。将此扩展到泛型方法似乎是一个棘手的子案例。

4.8 中的 JLS 片段(您引用)涵盖了构造函数、实例方法和成员字段 - 泛型方法通常只是实例方法的一个特例。因此,此代码段似乎涵盖了您的情况。

使 JLS 4.8 适应这种特定情况:

The type of a generic method is the raw type that corresponds to theerasure of its type in the generic declaration corresponding to C.

(这里方法的“类型”将包括所有参数和返回类型)。如果您将“删除”解释为“删除所有泛型”,那么这似乎与观察到的行为相匹配,尽管它不是很直观甚至没有用。删除所有泛型,而不仅仅是泛型类参数,这似乎是一种过分热心的一致性(尽管我是谁来猜测设计者)。

类泛型参数与方法泛型参数交互时可能会出现问题 - 在您的代码中,它们是完全独立的,但您可以想象将它们分配/混合在一起的其他情况。我认为值得指出的是,根据 JLS,不建议使用原始类型:

The use of raw types is allowed only as a concession to compatibilityof legacy code. The use of raw types in code written after theintroduction of genericity into the Java programming language isstrongly discouraged. It is possible that future versions of the Javaprogramming language will disallow the use of raw types

Java 开发人员的一些想法在这里很明显:

http://bugs.sun.com/view_bug.do?bug_id=6400189

(错误 + 修复显示方法的返回类型被视为方法类型的一部分,以便进行这种类型删除)

还有this request ,其中有人似乎要求您描述的行为 - 只删除类泛型参数,而不是其他泛型 - 但它被以下理由拒绝:

The request is to modify type erasure so that in the type declaration Foo<T>, erasure only removes T from parameterized types. Then, it so happens that within Map<K,V>'s declaration, Set<Map.Entry<K,V>> erases to Set<Map.Entry>.

But if Map<K,V> had a method that took type Map<String,V>, its erasure would just be Map<String>. For type erasure to change the number of type parameters is horrific, especially for compile-time method resolution. We are absolutely not going to accept this request.

It is too much to expect to be able to use raw types (Map) while still getting some of the type-safety of generics (Set<Map.Entry>).

关于泛型类中的 Java 泛型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18001550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com