简体   繁体   English

在Java通用编程中,Object类是没有通配符的参数类型的上限还是下限?

[英]In java generic programming, is the Object class upper bound or lower bound of the parameter type without wildcard?

when answering the question Creating generic array in Java via unchecked type-cast , newacct said 通过回答未经检查的类型转换在Java中创建通用数组的问题时,newacct说

(The lower bound of Bar is Object in this question. In a case where the lower bound of Bar is something else, replace all occurrences of Object in this discussion with whatever that bound is.) (Bar的下限是此问题中的对象。如果Bar的下限是其他内容,请使用该界限将本次讨论中所有出现的Object替换。)

Here is newacct's code: 这是newacct的代码:

class Foo<Bar> {
    Bar[] bars = (Bar[])new Object[5];
    public Bar get(int i) {
        return bars[i];
    }
    public void set(int i, Bar x) {
        bars[i] = x;
    }
    public Bar[] getArray() {
        return bars;
    }
}

I wonder whether Object is the upper bound or lower bound of Bar . 我想知道ObjectBar的上限还是下限。 I think that Foo<Bar> is short for Foo<Bar extends Object> , so Object should be the upper bound of Bar , am I wrong? 我认为Foo<Bar>Foo<Bar extends Object>缩写,因此Object应该是Bar的上限,我错了吗?

Given a type variable declaration <T extends TypeName> , TypeName is the upper bound of T . 给定类型变量声明<T extends TypeName>TypeNameT的上限。 Technically, I think it's just the bound , since the upper/lower distinction is only made for wildcards . 从技术上讲,我认为这只是界限 ,因为上下区别仅针对通配符 Additionally, if no bound is specified, then Object is assumed , so you're right that Object is the bound of Bar in the example. 另外, 如果未指定界限,则假定为Object ,因此您正确地认为Object是示例中Bar的界限。

I think newacct just misspoke when they wrote the answer and meant to say 'upper bound' instead of 'lower bound'. 我认为newacct在写下答案并打算说“上限”而不是“下限”时只是miss之以鼻。

Edit : Also, when talking about the particular construct in the referenced Q&A, it's useful to point out that the erasure of a type variable is the erasure of its left-most bound which is the importance of replacing the element type used for the array creation with the type used in the bound. 编辑 :另外,当谈到引用的问答中的特定构造时,指出一个类型变量的擦除是其最左边的擦除是很有用的,这是替换用于数组创建的元素类型的重要性绑定中使用的类型。 For example, given a complex type variable declaration <T extends Comparable<T> & Serializable> , the erasure of T would be Comparable , so you would use Comparable[] or Comparable<?>[] . 例如,给定复杂类型变量声明<T extends Comparable<T> & Serializable> ,对T的擦除将是Comparable ,因此您将使用Comparable[]Comparable<?>[]

You can define the lower bound as follows in generics as well. 您也可以在泛型中如下定义下限。

class Foo<T extends Bar> {
T[] bars = (T[])new Object[5];
public T get(int i) {
    return bars[i];
}
public void set(int i, Bar x) {
    bars[i] = x;
}
public T[] getArray() {
    return bars;
}

} }

So you can create a Foo object with any class that extends Bar. 因此,您可以使用任何扩展Bar的类来创建Foo对象。

No, you cannot use Object typed instances and lower bound is Bar and upper bound is any class that extends Bar class . 不,您不能使用对象类型的实例,下限是Bar ,上限是任何扩展Bar class的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM