泛型练习1(from李兴华)

package test.arithmetic;

public class Arithmetic1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Point<String> p=new Point<String>();
		p.setX("180度");
		p.setY("30度");
		System.out.println(p);
		
		Point<Integer> p1=new Point<Integer>();
		p1.setX(10);
		p1.setY(20);
		System.out.println(p1);
		
		Point<Double> p2=new Point<Double>(20d,40d);
		System.out.println(p2);
		
		Notepad<String,String> n=new Notepad<String,String>();
		n.setKey("Key");
		n.setValue("value");
		System.out.println("key:"+n.getKey()+"\t value:"+n.getValue());
	}
	
}

class Point<T>{
	private T x;
	private T y;
	
	public Point() {
	}
	
	public Point(T x, T y) {
		super();
		this.x = x;
		this.y = y;
	}

	public T getX() {
		return x;
	}
	public void setX(T x) {
		this.x = x;
	}
	public T getY() {
		return y;
	}
	public void setY(T y) {
		this.y = y;
	}
	@Override
	public String toString() {
		return "x坐标:"+x+" \t y坐标:"+y+" \t 类型:"+x.getClass();
	}
}

class Notepad<K,V>{
	private K key;
	private V value;
	
	public K getKey() {
		return key;
	}
	public void setKey(K key) {
		this.key = key;
	}
	public V getValue() {
		return value;
	}
	public void setValue(V value) {
		this.value = value;
	}
}

你可能感兴趣的:(java,泛型)