holder3.java

来自「这是Java思想的最高集萃」· Java 代码 · 共 33 行

JAVA
33
字号
package chapter15;

public class Holder3<T> {
	private T a;
	public Holder3(T a){
		this.a=a;
	}
	
	public void set(T a){
		this.a=a;
		System.out.println("set!");
	}
	
	public T get(){
		System.out.println("get!");
		return a;
	}
	
	public static void main(String args[]){
		Holder3<Automobile1>  h1=new Holder3<Automobile1>(new Automobile1());
		Automobile1 a =h1.get();
		h1.set(a);
		/*
		 * 我们看到,由于使用了泛型,类Holder3的对象就可以使用类Automobile1和Automobile2的
		 * 方法了。这样,类Holder3就使用了两种类型。
		 */
		Holder3<Automobile2>  h2=new Holder3<Automobile2>(new Automobile2());
		Automobile2 b =h2.get();
		h2.set(b);
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?