泛型优点
增加了代码的安全性
省去了代码强制转换的麻烦.
泛型可以把代码运行时候的错误 显示在编译期
泛型的位置
public static void main(String []agrs){
ArrayList<String> arrayList = new ArrayList<String>();
泛型就在<>这对尖括号中
在创建对象的时候就可以给泛型附一个值
}
泛型是怎么来的呢?
因为在人写代码的时候语法没有错误编译软件是不会报错的但是,有的错误是运行时才会提示
为了将这些方便所以有了泛型
泛型可以把代码运行时候的错误 显示在编译期。
写泛型的时候需要注意些什么???
下面我们一起来写一个泛型
ArrayList<String> arrayList = new ArrayList<string>();
arrayList.add("d");
arrayList.add("c");
ListItereter<string> listItereter = new arrayList.ListItereter();
while(listItereter.hasNext();){
object next = listItereter next();
System.out.println(next) ;
}
接中的泛型 在哪里声明?
我们一起写一个接口
interface Inter<T>{
//注意接口中<>将括号里只可以写大写字母
public abstract void fun(T,t);
}
public fun2 implements Inter<T>{
public void fun3(T,t)
System.out.println(t);
}