1.泛型類java
public class Dog<T> { private T age; public Dog(T age) { this.age = age; } public T getAge() { return age; } public static void main(String[] args) { //Java7以後,尖括號中是不須要填寫參數的 Dog<String> dog=new Dog<>("28"); System.out.println(dog.getAge()); } }
普通的類安全
public class Dog { private Object age; public Dog(Object age) { this.age = age; } public Object getAge() { return age; } public static void main(String[] args) { Dog dog=new Dog("28"); System.out.println(dog.getAge()); } }
這樣的代碼是徹底能夠執行了,那爲何還須要泛型類?app
1.安全性ide
public class Dog { private Object age; public Dog(Object age) { this.age = age; } public Object getAge() { return age; } public static void main(String[] args) { Dog dog=new Dog("28"); Integer age=(Integer) dog.getAge(); System.out.println(age); } }
上面的代碼編譯是徹底能夠經過的,可是執行的時候就會出現ClassCastException異常
2.可讀性好,省去了反覆的強制類型轉換。測試
對於泛型類,java編譯器會將泛型代碼轉換成普通的非泛型代碼,ui
因此對於虛擬機來講,是沒有泛型類的概念的。
爲何這麼設計呢?應爲泛型是jdk6之後纔有的,爲了向下兼容。this
泛型方法:spa
public class TestMethod { public static <T> boolean isHas(T[] arr, T elemt){ for(T t:arr){ if(t.equals(elemt)){ return true; } } return false; } public <S> boolean isString(S s){ if(s instanceof String){ return true; } return false; } public static void main(String[] args) { Integer[] arr={1,5,6,8}; System.out.println(isHas(arr,8)); TestMethod testMethod=new TestMethod(); System.out.println(testMethod.isString(5)); } }
一個方法是否是泛型和他的類是否是泛型沒有任何關係。
泛型方法須要在方法的返回值前先聲明,在從後面的代碼中使用。設計
泛型接口:
參照Comparable接口。code
public class TestComparable implements MyComparable<TestComparable>{ private Integer n; public TestComparable(int n) { this.n = n; } @Override public boolean isEquals(TestComparable testComparable) { return this.n.intValue()==testComparable.getN().intValue()?true:false; } public Integer getN() { return n; } public static void main(String[] args) { TestComparable testComparable1=new TestComparable(2); TestComparable testComparable2=new TestComparable(2); System.out.println(testComparable1.isEquals(testComparable2)); } } interface MyComparable<T> { boolean isEquals(T t); }
類型參數繼承某個類
/** * 測試繼承class */ public class TestInheritClass<T extends Father>{ private T t; public TestInheritClass(T t) { this.t = t; } void output(){ System.out.println(t.getName()); } public static void main(String[] args) { Child child=new Child("李逵"); TestInheritClass<Child> t=new TestInheritClass<>(child); t.output(); } } class Father{ private String name; public String getName() { return name; } public Father(String name) { this.name = name; } } class Child extends Father{ public Child(String name) { super(name); } }
測試繼承接口
/** * 測試繼承接口 */ public class TestInheritInterface<T extends IFruits> { private T t; public TestInheritInterface(T t) { this.t = t; } void output(){ t.shape(); } public static void main(String[] args) { Apple apple=new Apple(); TestInheritInterface<Apple> t=new TestInheritInterface<>(apple); t.output(); } } interface IFruits{ //形狀 void shape(); } class Apple implements IFruits{ @Override public void shape() { System.out.println("蘋果是圓形的。"); } }