java泛型深度 理解

来源:互联网 发布:java swing图形界面 编辑:程序博客网 时间:2024/06/06 11:47

泛型总的来说,指不确定的类型

其中掐面三个不叫泛型,但是类似泛型;后面一个代指一些不确定类型是泛型(差不多和模板类似)。

1.下面用到了一个泛型的示例:

/*
* 泛型
*/
public class Test {
public static void main(String args[]){
  Person per[]=new Person[5];
  per[0]=new Student();
  per[1]=new Employee();
  ((Student) per[0]).print();
  ((Employee) per[1]).print();
}
}

class Person{

}


class Employee extends Person{
public void print(){
  System.out.println("i am employee");
}
}

class Student extends Person{
public void print(){
  System.out.println("i am Studnent");
}
}

其中Person就是泛型类.

2.依次类推大家用到Object超类时也是用到了泛型(就是可以代指一切,但是不能代指基本数据类型,这也是为什么要将基本数据类型进行封装称对应的封装类)。

3.还有大家用到的接口(只有实现了这个接口,才可以用这个接口的泛型),如下面示例:

/*
* 泛型
*/
public class Test {

public static void main(String args[]){
  Area area[]=new Area[4];
  area[0]=new circle(3);
  area[1]=new SanJiao(3,4);
  area[2]=new BuPi();
  System.out.println(area[0].getArea());
  System.out.println(area[1].getArea());
  System.out.println(area[2].getArea());
}
}

interface Area{
public float getArea();
}

/*
* 形状的基类
*/
class Shape{}

/*
* 这是圆形
*/
class circle extends Shape implements Area{

private int r;

public circle(int r){
  this.r=r;
}

public float getArea() {
  // TODO Auto-generated method stub
  return (float) (3.14*r*r);
}
}

/*
* 这是三角形
*/
class SanJiao extends Shape implements Area{
private int l;
private int h;
public SanJiao(int l,int h){
  this.l=l;
  this.h=h;
}

public float getArea() {
  // TODO Auto-generated method stub
  return h*l/2;
}
}

/*
* 这是一张布匹,他也有面积
*/
class BuPi implements Area{

public float getArea() {
  // TODO Auto-generated method stub
  return 5;
}
}

 

4.用模板的方式来运用泛型:

java的模板类可以理解为含有Object类型的类。

<1. java的模板类的模板参数只能是参数类型,成员变量类型等,模板名是确定的。

<2. 运行期,模板参数会被当作Object来处理,已经验证

<3. 使用模板类的类型安全,只是利用编译器的类型检查,来自动保证运行期的类型强转的正确与安全。

示例如下:

[java] view plaincopyprint?
  1. class  TestTemplate 
  2.     public staticvoid main(String[] args)  
  3.     { 
  4.         //vector<T> v=new vector<T>(); 
  5.         Byte b=new Byte((byte)0); 
  6.         vector<Byte> v=new vector<Byte>(b); 
  7.         v.add((byte)0); 
  8.         v.add((byte)1); 
  9.         v.add((byte)3); 
  10.         v.add((byte)2); 
  11.         v.visitAll(); 
  12.          
  13.     } 
  14.  
  15. class vector<T>{ 
  16.  
  17.     private int size=0
  18.     private vectorElement<T> head=null
  19.     private vectorElement<T> last=null
  20.  
  21.     public vector(T datum) 
  22.     { 
  23.         System.out.println("vector(T datum)!"); 
  24.         this.head=new vectorElement<T>(datum); 
  25.         this.last=this.head; 
  26.         this.size=1;  
  27.     } 
  28.  
  29.  
  30. public boolean add(T datum){ 
  31.  
  32.         if(this.size==0){ 
  33.             this.head=new vectorElement<T>(datum); 
  34.             this.last=this.head; 
  35.             this.size=1
  36.         }else
  37.             vectorElement<T> temp=new vectorElement<T>(datum); 
  38.             temp.previous=this.last; 
  39.             this.last.next=temp; 
  40.             this.last=temp; 
  41.             this.size=this.size+1
  42.         } 
  43.         return true
  44.  
  45.     } 
  46.  
  47.     public void visitAll() 
  48.     { 
  49.         vectorElement<T> walker; 
  50.         walker=head; 
  51.         while(walker !=null
  52.         { 
  53.             System.out.println(walker.datum); 
  54.             walker=walker.next; 
  55.         } 
  56.     } 
  57.  
  58. class vectorElement<T>{ 
  59.  
  60.     public vectorElement<T> next=null
  61.     public vectorElement<T> previous=null
  62.     public T datum=null
  63.  
  64.     public vectorElement(T datum){this.datum=datum; } 
  65.  
  66.  
  67. /*
  68. vector(T datum)!
  69. 0
  70. 0
  71. 1
  72. 3
  73. 2
  74. 请按任意键继续. . .
  75. */ 

 

 

0 0