ITeye博客

Java Generic Types

ymanyang · 2012-11-16 18:10 · Java · 编程语言 · Markdown

A generic type is a generic class or interface that is parameterized over types. It can be used to generalize the type of parameters and return value in coding without the risk to pass a wrong type to its methods. Like the following example:

/**
 * Generic version of the Box class.
 * @param <T> the type of the value being boxed
 */
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

 When initiate this class or use it, it only need to replace the type parameter with desired class type.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。