您的位置:首页 > 编程语言 > Java开发

Java泛型二:泛型类 泛型接口 泛型方法详解

2016-07-13 17:22 453 查看

泛型类

单参数泛型类:

public class Rectangle<T> {
private T width;
private T height;

public Rectangle(T width, T height){
this.width = width;
this.height = height;
}

public T getWidth() {
return width;
}

public void setWidth(T width) {
this.width = width;
}

public T getHeight() {
return height;
}

public void setHeight(T height) {
this.height = height;
}

public static void main(String [] args){
Integer width = 10;
Integer height = 5;
Rectangle<Integer> rect = new Rectangle<Integer>(width, height);

String widthStr = "20";
String  heightStr = "40";
Rectangle<String> rect1 = new Rectangle<String>(widthStr, heightStr);
}
}


通过
public class Rectangle<T> {}
定义泛型类,在实例化该类时,必须指明泛型T的具体类型,例如:
Rectangle<String> rectangle = new Rectangle<String>();
,指明泛型T的类型为String。

多参数泛型类:

public class Container<K, V> {
private K key;
private V value;

public Container(K k, V v) {
key = k;
value = v;
}

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;
}

public static void main(String[] args) {
Container<String, String> c1 = new Container<String, String>("name", "Messi");
Container<String, Integer> c2 = new Container<String, Integer>("age", 30);
Container<String, Double> c3 = new Container<String, Double>("height", 1.78);
System.out.println(c1.getKey() + " : " + c1.getValue());
System.out.println(c2.getKey() + " : " + c2.getValue());
System.out.println(c3.getKey() + " : " + c3.getValue());
}
}


通过
public class Container<K, V> {}
定义泛型类,在实例化该类时,必须指明泛型K、V的具体类型,例如:
Container<String, String> c1 = new Container<String, String>("name", "Messi");
,指明泛型K的类型为String,泛型V的类型为String。

泛型接口

public interface Calculator<T> {
public T and(T a, T b);
}


public class CalculatorInteger implements Calculator<Integer>{
public Integer and(Integer a, Integer b){
return a + b;
}

public static void main(String[] args) {
CalculatorInteger ci = new CalculatorInteger();
Integer val = ci.and(10, 20);
System.out.println(val);
}
}


public class CalculatorString implements Calculator<String>{
public String and(String a, String b){
return a + b;
}

public static void main(String[] args) {
CalculatorString ci = new CalculatorString();
String val = ci.and("10", "20");
System.out.println(val);
}
}


通过
public interface Calculator<T> {}
定义泛型接口,在实现该接口时,必须指明泛型T的具体类型,例如:
public class CalculatorInteger implements Calculator<Integer>{}
,指明泛型T的类型为Integer,实例化该类时无需制定泛型类型。

泛型方法

public class Generic {
public <T> T getObject(Class<T> c){
T t = null;
try {
t = c.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return t;
}

public static void main(String [] args){
try {
Generic generic = new Generic();
Object obj = generic.getObject(Class.forName("com.ips.volatiles.demo2.Generic"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


泛型方法说明:



在调用泛型方法的时候,在不指定泛型的情况下,泛型变量的类型为该方法中的几种类型的同一个父类的最小级,直到Object。在指定泛型的时候,该方法中的几种类型必须是该泛型实例类型或者其子类。

在初始化泛型类的时候,不指定泛型的时候,泛型的类型为Object,就比如ArrayList中,如果不指定泛型,那么这个ArrayList中可以放任意类型的对象。

泛型的好处是在编译的时候进行类型安全检查,并且所有的强制转换都是自动和隐式的,以提高代码的重用率。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息