上课是什么意思。当我们需要这种课程的时候。例如
class SimpleCounter<T> { /*...*/ }SimpleCounter<Double> doubleCounter = new SimpleCounter<Double>();
class SimpleCounter<T> { /*...*/ }
SimpleCounter<Double> doubleCounter = new SimpleCounter<Double>();
2vuwiymt1#
请参见泛型类型泛型类的定义格式如下: class name<T1, T2, ..., Tn> { /* ... */ } 类型参数部分由尖括号(<>)分隔,紧跟在类名之后。它指定类型参数(也称为类型变量)t1、t2、…、和tn。要更新box类以使用泛型,可以通过更改代码来创建泛型类型声明“ public class Box 到“公共类框”。这引入了类型变量t,它可以在类中的任何地方使用。通过此更改,box类将变为:
class name<T1, T2, ..., Tn> { /* ... */ }
public class Box
/** * 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; }}
/**
* 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; }
}
1条答案
按热度按时间2vuwiymt1#
请参见泛型类型
泛型类的定义格式如下:
class name<T1, T2, ..., Tn> { /* ... */ }
类型参数部分由尖括号(<>)分隔,紧跟在类名之后。它指定类型参数(也称为类型变量)t1、t2、…、和tn。要更新box类以使用泛型,可以通过更改代码来创建泛型类型声明“
public class Box
到“公共类框”。这引入了类型变量t,它可以在类中的任何地方使用。通过此更改,box类将变为: