黑马程序员_泛型概述

来源:互联网 发布:电脑防蓝光的软件 编辑:程序博客网 时间:2024/04/26 19:45

------- android培训、java培训、期待与您交流! ----------


泛型:jdk1.5以后出现的新特性。用于解决安全问题,是一个类型安全机制

好处:
1.将运行期间出现问题ClassCastException,转移到编译期间,方便程序员解决问题 让运行事情问题减少,安全
2.避免了强制转换的麻烦

泛型格式:

通过<>来定义要操作的引用数据类型
在使用java提供 的对象时 什么时候写泛型

通常在集合框架中很常见  只要见到<>就要定义泛型 其实<>就是用来接收类型的
当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可

什么时候使用泛型类型类
当类中要操作的引用类型数据不确定时,早期定义Object来完成扩展  现在定义泛型来完成扩展。泛型定义的泛型,

在整个类中有效。如果被方法使用。那么泛型类的对象明确要操作的类型后,所有要操作的类型就已经固定了。

为了让不同方法可以操作不同类型,而且类型还不确定,那么可以讲泛型定义在方法上

特殊之处:
1.静态方法不可以访问类上定义的泛型。
如果静态方法操作的应用数据类型不确定,可以讲泛型定义在方法上

-----------------------------------------------------------------------------------------------------------

public class CollectionDemo {
public static void main(String[] args) {
        Demo<String> d=new Demo<String>();
        d.show("haha");     //haha   从输出结果可以看出泛型既可以定义类上 也可以定义在方法
        d.print(5);       //5
}
}

-------------------------------------------------------------------------------
class Demo<T>{
public void show(T t){  //当接收类型不确定时可以用大写字母表示
System.out.println("show:"+t);//与父类相同
}
public<Q> void print(Q q){   //不同方法接受的类型不一样
System.out.println("print:"+q);
}
}

-----------------------------------------------------------------------------------------------------------
2.泛型定义在接口上 

-----------------------------------------------------------------------------------------------------------

public class GenericDemo {
public static void main(String[] args) {
       InterImpl i=new  InterImpl();
       i.show("sss");    //sss  在方法可以不设定具体类型 而在创建对象时传入
}
}

-----------------------------------------------------------------------------
interface Inter<T>       ///泛型可以设定在接口上
{
void show(T t);
}
class InterImpl implements Inter<String>{    //在实现接口时 设定接受类型
public void show(String t) {
System.out.println("show:"+t);
}
}

-------------------------------------------------------------------------------------------------------------


?通配符,也可以理解为占位符
泛型的限定:
?exteds E:可以接受E类型或者E的子类型。上限
?super E:可以接收E类型或者E的父类型。下限

通配符?和接受T类型的区别:

T是具体类型,而?是占位符,不是具体类型。


------------------------------------------------------------------------------------------------------------------------------------------

public class GenericDemo06 {
public static void main(String[] args) {
ArrayList<Person03> al=new ArrayList<Person03>();   //ArrayList的引用数据类型为Person03
   al.add(new Person03("abc01"));
   al.add(new Person03("abc02"));
   al.add(new Person03("abc03"));
   
   ArrayList<Student03> al2=new ArrayList<Student03>();  //ArrayList的引用数据类型为Student03
   al2.add(new Student03("abc===01"));
   al2.add(new  Student03("abc==02"));
   al2.add(new Student03("abc=03"));

   printColl(al);
   printColl(al2);

---------------------------------------------------------

打印结果:

abc01

abc02

abc03

abc===01

abc===02

abc===03

---------------------------------------------------------
}
public static void printColl(ArrayList<? extends Person03> al2){      / /向上限定   Person03及其子类  皆可打印
Iterator<? extends Person03> it=al2.iterator();
while(it.hasNext()){
System.out.println(it.next().getName());
}
  }
}

--------------------------------------------------------------------------------------------------------
class Person03{      //父类Person03
private  String name;
Person03(String name){
this.name=name;
}
public String getName(){
return name;
}
}

--------------------------------------------------------------------------------------------------------
class Student03 extends Person03   //jStudent03子类继承父类
{
Student03(String name) {
super(name);
}
}

------------------------------------------------------------------------------------------------------------------------------------------


------- android培训、java培训、期待与您交流! ----------

详细请查看:http://edu.csdn.net/heima/