简单泛型

嵌套咯

ROOM  ----》RoomBox

RoomBox

public class RoomBox<T> {
	private Box<T> box;
	
	public Box<T> getBox(){
		return box;
	}
	
	public void setBox(Box<T> box){
		this.box = box;
	}
}

Room

public class Room<T> {
	private T thing;
	
	public T getThing(){
		return thing;
	}
	
	public void setThing(T thing){
		this.thing = thing;
	}
	
	@Override
	public String toString(){
		return "Room[thing=" + thing + "]";
	}
}
测试类


public class RoomBoxTest {
	public static void main(String[] args){
		RoomBox<String> roomBox = new RoomBox<>();
	
		Box<Integer> box = new Box<>();
		
		Box<String> boxString = new Box<>();
	
		roomBox.setBox(boxString);
		
		// 注意 泛型 类型 必须 一致
		RoomBox<Box<String>> room = new RoomBox<>();
		Box<Box<String>> boxBox = new Box<>();
		
		room.setBox(boxBox);
	}
}


猜你喜欢

转载自blog.csdn.net/fyangfei/article/details/78797727