当前位置: 移动技术网 > IT编程>开发语言>Java > java泛型详解

java泛型详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

首先请看如下代码:

public class generictype {
 public static void main(string str[]) {
  hashtable h =new hashtable();
  h.put(1, "string类型");
  int a = (string) h.get(1);
  system.out.println(a);
 }
}
//执行结果
string类型
//如果我们将上述由红色标出的string改为int执行后结果如下(更改后编译没有错误):
exception in thread "main" java.lang.classcastexception: java.lang.string cannot be cast to java.lang.integer
 at genetictype.generictype.main(generic1.java:10)

以上就是强制类型转换可能带来的典型错误,然而这个错误在编译期间无法知道,以至于在运行期间jvm检查后抛出类型转换异常。

再看下述代码:

public class generictype { 
 public static void main(string str[]) {  
 hashtable<integer, string> h = new hashtable<integer, string>();
  h.put(1, "string类型");
  string a= h.get(1);
  system.out.println(a);
 }
}
//执行结果
string类型
//需要提出的是1.上述由红色标出的string如果改为int,在编译的时候会报错
   2.在h.get(1)前面不需要再进行强制类型转换。

综上看来泛型的作用为:

1.就是是在编译的时候检查类型的安全(解决java中强制类型转换可能导致的错误),交给了编译器巨大的使命。 

2.提高代码的重用率

类型擦除:

类型擦除就是说编译器编译.java文件时,将类的泛型参数去掉,那么jvm加载字节码文件的时候对泛型不可见,这个过程就称为类型擦除。

与类型擦除有关的现象:

(1) 泛型类没有class的类类型。比如并不存在list<string>.class或是list<integer>.class,而只有list.class。

(2) 静态变量是被泛型类的所有实例所共享的。

public class generictype 
{
 public static void main(string str[]){
  test1<string> t = new test1<string>();
  test1<date> tt = new test1<date>();
  system.out.println(t.a);
  system.out.println(tt.a);
 }
}
class test1<t>{
 static int a = 1;
}
//结果
1

(3) 泛型的类型参数错误不能通过异常处理,因为异常处理是jvm实现的,而jvm加载的字节码文件已经擦除了泛型特征,这也间接的说明了泛型的意义:在编译期间发现参数类型错误。

类型擦除的基本过程也比较简单:

 1.将类型参数用顶级父类替换,这类一般是object,如果指定了类型参数的上界的话,则使用这个上界。

 2.去掉出现的类型声明,即去掉<>的内容。

例如:t get()方法声明就变成了object get();list<string>就变成了list。接下来就可能需要生成一些桥接方法(bridge method)。这是由于擦除了类型之后的类可能缺少某些必须的方法。比如考虑下面的代码:

public class generictype {public static void main(string str[]) {
  test3 t =new test3();
   t.gett("11111");
 }
}
interface test2<t>{
 public t gett(t t);
}
class test3 implements test2<string>{
 public string gett(string t){
  return t;
 }
}
//类型擦除后的代码
public class generictype {
 public static void main(string str[]) {
  test3 t = new test3();
  t.gett("11111");
 }
 interface test2 {
  public object gett(object t);
 }
 class test3 implements test2 {
 public string gett(string t){
   return t }
 public object gett(object t) {
  return this.gett((string) t);
  }//如果没有这段代码,在类型擦除后test3没有重写接口test2的抽象方法,明显错误,因此编译器的巨大作用就是在这里帮忙生成了该方法,同时编译器也依靠该功能完成检错任务。
}

泛型的分类:泛型类,泛型接口,泛型方法,泛型异常

泛型类

public class generictype {
 public static void main(string str[]) {
  test<integer, string> t = new test<integer, string>();
  t.put(1, "str1");
  t.put(2, "str2");
  system.out.println(t.get(1));
  system.out.println(t.get(2));
 }
}
class test<t, v> {
 public hashtable<t, v> h = new hashtable<t, v>();
 public void put(t t, v v) {
  h.put(t, v);
 }
 public v get(t t) {
  return h.get(t);
 }
}
//执行结果
str1
str2

多态方法(泛型方法):在函数名前定义泛型参数,可以在传入参数列表,返回值类型,方法体里面引用

public class generictype {
public <t> string getstring(t obj){
 return obj.tostring();
}
 public static void main(string str[]) {
  generictype g =new generictype ();//不需要类的泛型
  system.out.println(g.getstring(1));
  system.out.println(g.getstring('a'));
  system.out.println(g.getstring("a"));
 }
}
//执行结果
a
a

泛型异常(兼具泛型接口)

public class generictype {
 public static void main(string str[]) {
 testexception t =new testexception();
 try {
  t.excute(2);
 } catch (ioexception e) {
  e.printstacktrace();
 }
 }
}
//extends说明该泛型参数继承于exception
interface testexceptioninterface<t extends exception>
{
 public void excute(int i) throws t;
}
class testexception implements testexceptioninterface<ioexception>{
 @override
 public void excute(int i) throws ioexception {
 if(i<10){
  throw new ioexception();
 }
 }
}
//意义:1.针对不同的可能出现的异常类型,定义自己的实现类。
  2.定义多个实现类的时候,不用一个一个手动throws异常,提高了代码重用率

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网