`

More Fun with Wildcards and <T> before method return type T

 
阅读更多

In this section, we’ll consider some of the more advanced uses of wildcards. We’ve

seen several examples where bounded wildcards were useful when reading from a data
structure. Now consider the inverse, a write-only data structure.
The interface Sink is a simple example of this sort.

 

interface Sink<T> {
    flush(T t);
}

 

We can imagine using it as demonstrated by the code below. The method writeAll()
is designed to flush all elements of the collection coll to the sink snk, and return the
last element flushed.

 

public static <T> T writeAll(Collection<T> coll, Sink<T> snk) { 
    T last;
    for (T t : coll) { 
        last = t;
        snk.flush(last);
    }
    return last;
}
...
Sink<Object> s;
Collection<String> cs;
String str = writeAll(cs, s); // illegal call

 

As written, the call to writeAll() is illegal, as no valid type argument can be inferred;
neither String nor Object are appropriate types for T, because the Collection element
and the Sink element must be of the same type.
We can fix this by modifying the signature of writeAll() as shown below, using a
wildcard.

 

public static <T> T writeAll(Collection<? extends T>, Sink<T>) {
    ...
}
... 
String str = writeAll(cs, s); // call ok, but wrong return type
 

The call is now legal, but the assignment is erroneous, since the return type inferred
is Object because T matches the element type of s, which is Object.
The solution is to use a form of bounded wildcard we haven’t seen yet: wildcards
with a lower bound. The syntax ? super T denotes an unknown type that is a
supertype of T3. It is the dual of the bounded wildcards we’ve been using, where we
use ? extends T to denote an unknown type that is a subtype of T.

 

public static <T> T writeAll(Collection<T> coll, Sink<? super T> snk) {
    ...
}
...
String str = writeAll(cs, s); // Yes!

 

Using this syntax, the call is legal, and the inferred type is String, as desired.

 

and i can see that the <T> used by the Type or method describes the identical T usage within its domain.

分享到:
评论

相关推荐

    java反编译工具jad 1.5.8g(可以反编译jdk1.5,1.6)

    sjava *.class&lt;br&gt;&lt;br&gt; (or jad -o -d test -s java *.class, which has the same effect)&lt;br&gt;&lt;br&gt;This command decompiles all .class files in the current directory &lt;br&gt;and places all output files with ...

    Java1.5泛型指南中文版(Java1.5Gene....pdf

    例如,`public static &lt;T&gt; T identity(T t) { return t; }` 定义了一个名为 identity 的泛型方法。 6. 与旧代码交互 泛型代码可以与旧代码交互,例如使用老代码中的方法或变量。但是,需要注意类型安全问题,避免...

    Efficient string matching with wildcards and length constraints

    文章提出了一种名为SAIL1(String Matching with Wildcards and Length Constraints)的高效算法,该算法能在非线性时间内返回模式`P`在文本`T`中的每一次出现,并且只需要线性的额外空间。具体来说,对于长度为`n`...

    Java泛型_Java中的泛型结构_

    - 实例化:`MyClass&lt;String&gt; myObj = new MyClass&lt;&gt;();` - 类型擦除:Java编译器会进行类型擦除,将泛型类的实例转换为无参数类型,但会在编译时进行类型检查。 3. 泛型接口: - 定义与实例化与泛型类类似,例如...

    Java1.5泛型指南中文版

    More fun with Wildcards #### 通配符匹配 通配符匹配是指在某些情况下,Java编译器可以自动推导出通配符的类型。例如,在将一个`List&lt;Integer&gt;`传递给接受`List&lt;? extends Number&gt;`的方法时,编译器能够自动推导...

    Java1.5泛型指南中文版.doc

    More fun with * 通配符可以用来表示不确定的类型,但在某些情况下,Java会自动推导出具体的类型,这种现象被称为“通配符捕获”。 #### 10. 泛型化老代码 将现有非泛型代码升级为泛型代码时,需要逐步进行,确保...

    java泛型pdf资料

    MyGenericClass&lt;String&gt; stringContainer = new MyGenericClass&lt;&gt;(); stringContainer.setElement("Hello"); String value = stringContainer.getElement(); ``` #### 三、泛型和子类继承 Java 泛型支持子类继承。...

    java+泛型.pdf

    List&lt;Integer&gt; myIntList = new LinkedList&lt;&gt;(); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next(); ``` 在泛型版本中,类型转换是不必要的,编译器会自动检查类型一致性,避免了运行时可能...

    java泛型指南 经典

    Box&lt;String&gt; stringBox = new Box&lt;&gt;(); stringBox.setItem("Hello, World!"); String s = stringBox.getItem(); ``` ##### 3.2 使用泛型方法 除了定义泛型类之外,我们还可以定义泛型方法。泛型方法可以在非泛型类...

    Java反编译软件JAD1

    -pp &lt;pfx&gt;- prefix for method parms with numerical names (default: _prm) -r - restore package directory structrure -s &lt;ext&gt; - output file extension (by default '.jad') -stat - display the total ...

    java1.5范型编程指南

    9. **More fun with `*`** - **通配符匹配(wildcard capture)**:在某些情况下,编译器会自动捕获通配符并创建一个新的匿名类型,以实现更灵活的类型操作。 10. **泛型化老代码** - 对于已有的非泛型代码,可以...

    使用通配符简化泛型使用1

    extends T&gt;`表示可以接受T或T的任何子类型,而`&lt;? super T&gt;`则表示可以接受T或T的任何超类型。这些通配符在方法参数中尤其有用,允许方法处理特定类型范围的对象。 6. 捕获通配符: 在方法签名中,可以使用多个...

    JAVA泛型详解[参考].pdf

    在类的实例化时,我们可以指定`T`的具体类型,如`Position&lt;Integer&gt;`或`Position&lt;String&gt;`,这样编译器就能确保插入和获取的值都是指定的类型。例如: ```java public class Position&lt;T&gt; { private T x; private ...

    jdk1.5中的范型

    public static &lt;T&gt; void swap(T[] array, int i, int j) { T temp = array[i]; array[i] = array[j]; array[j] = temp; } } ``` #### 与旧代码交互 在使用泛型时,需要考虑与未使用泛型的旧代码交互的问题。...

    Generic in java programming language

    public static &lt;T&gt; T max(T[] array) { T max = array[0]; for (T element : array) if (element.compareTo(max) &gt; 0) max = element; return max; } } ``` 这个`max`方法可以用于任何实现了`Comparable`...

    PLSQL.Developer v11.0.4.1774 主程序+ v11中文包+keygen

    REPORT LOCKFORALL &lt;path&gt; &lt;password&gt; [R] REPORT LOCKFOROTHERS &lt;path&gt; &lt;password&gt; [R] REPORT UNLOCK &lt;path&gt; &lt;password&gt; [R] REPORT LIST &lt;path&gt; [R] For the path you can use wildcards or directories. Add ...

Global site tag (gtag.js) - Google Analytics