定义:参数化类型 就是给类一个形参 类似于方法一样
List<String> list String是它的形参 而加入进去的具体字符串是他的实参
作用:进行类型检测 防止类型转换异常 将运行时的错误提前到编译期
List<String> list = new ArrayList<String>();
list.add("wyq");
list.add("hello");
list.add(56); // 这里编译错误
作用范围:泛型只作用于编译器 在运行期间不起作用
通配符:Integer与Number存在继承关系, 但是List<Integer> 与List<Number>不存在继承关系,可以使用通配符实现继承关系extends上限 super下限
例如 List<T extends Number> 所有继承Number的子类都可以存入这个List中
<span style="font-family:Courier New;font-size:14px;"> public class Solution {
</span><span style="font-family: 'Courier New';font-size:14px;"> class MyNumber<T extends Number>{</span>
<span style="font-family:Courier New;font-size:14px;"> private T t;
public void set</span><span style="font-family: 'Courier New'; font-size: 14px;">Number</span><span style="font-family: 'Courier New';">(T t){</span><span style="font-family:Courier New;font-size:14px;">
this.t = t;
}
public T get</span><span style="font-family: 'Courier New'; font-size: 14px;">Number</span><span style="font-family: 'Courier New';">(){</span><span style="font-family:Courier New;font-size:14px;">
return this.t;
}
}</span>
<span style="font-family:Courier New;font-size:14px;"> public static void main(String[] args){
Solution s = new Solution();
Solution.MyNumber p = s.new MyNumber<Integer>();//内部类的实例化方法
p.setNumber(123);
System.out.print((Integer)p.getNumber());</span><pre name="code" class="java"><span style="font-family:Courier New;font-size:14px;"> }</span>
<span style="font-family:Courier New;font-size:14px;">}</span>
泛型类:
<span style="font-family:Courier New;font-size:14px;">public class Person<T>{
private T t;
public void setPerson(T t){
this.t = t;
}
public T getPerson(){
return this.t;
}
}</span>
实现两个数相加
<span style="font-family:Courier New;font-size:14px;"> /** 通过使用泛型 两个数字相加 无论什么类型都可以
* @param t1
* @param t2
* @param <T>
*/
public <T extends Number> void sum(T t1,T t2){
if(t1.getClass()==Integer.class&&t2.getClass()==Integer.class){
System.out.print("Integer:");
System.out.print( t1.intValue() + t2.intValue());
}else if(t1.getClass()==Float.class&&t2.getClass()==Float.class){
System.out.print("Float:");
DecimalFormat format = new DecimalFormat(".00"); //保留两位小数
System.out.print(format.format(t1.floatValue() + t2.floatValue()));
}else if(t1.getClass()==Double.class&&t2.getClass()==Double.class){
System.out.print("Double:");
DecimalFormat format = new DecimalFormat(".000"); //保留三位小数
System.out.print(format.format(t1.doubleValue() + t2.doubleValue()));
} else if(t1.getClass()==Long.class&&t2.getClass()==Long.class){
System.out.print("Long:");
System.out.print( t1.longValue() + t2.longValue());
}else if(t1.getClass()==Short.class&&t2.getClass()==Short.class){
System.out.print("Short:");
System.out.print( t1.shortValue() + t2.shortValue());
}else if(t1.getClass()==Double.class||t2.getClass()==Double.class){
System.out.print("强制转换Double:");
DecimalFormat format = new DecimalFormat(".000");
System.out.print(format.format(t1.doubleValue() + t2.doubleValue()));
}else if(t1.getClass()==Long.class||t2.getClass()==Long.class){
System.out.print("强制转换Long:");
System.out.print(t1.longValue() + t2.longValue());
} else if(t1.getClass()==Float.class||t2.getClass()==Float.class){
System.out.print("强制转换Float:");
DecimalFormat format = new DecimalFormat(".00");
System.out.print(format.format(t1.floatValue() + t2.floatValue()));
}else if(t1.getClass()==Integer.class||t2.getClass()==Integer.class){
System.out.print("强制转换Integer:");
System.out.print(t1.intValue() + t2.intValue());
}
}</span>