五千年(敝帚自珍)

主题:【评论】Sun发布了最新的Java版本-- 5.0 Tiger版 -- Highway

共:💬18
全看分页树展 · 主题 跟帖
家园 Java 1.5的Generic是如何实现的

今天忽然有兴趣,写了两个很简单的Java 1.5程序,用Java 1.5来编译,然后用DJ来反编译所生成的Class文件,以此试试它的Generic是如何实现的,发现整个一个坑蒙拐骗。下面是各自的源程序和反编译出来的程序以及有关说明。

第一个程序的源程序:

import java.util.*;

public class MyClass {

public int sum(int data1,int data2) {

return data1 + data2;

}

public void test()

{

ArrayList<String> al=new ArrayList<String>();

al.add("111");

al.add("222");

System.out.println(al.get(al.size()-1));

System.out.println(al.get(al.size()-2));

ArrayList<Integer> al2=new ArrayList<Integer>();

al2.add(111);

al2.add(222);

System.out.println(al2.get(al2.size()-1));

System.out.println(al2.get(al2.size()-2));

}

public static void main(String[] args) {

MyClass mc=new MyClass();

System.out.println(mc.sum(10,20));

mc.test();

}

}

对应的反编译程序:

import java.io.PrintStream;

import java.util.ArrayList;

public class MyClass

{

public MyClass()

{

}

public int sum(int i, int j)

{

return i + j;

}

public void test()

{

ArrayList arraylist = new ArrayList();

arraylist.add("111");

arraylist.add("222");

System.out.println((String)arraylist.get(arraylist.size() - 1));

System.out.println((String)arraylist.get(arraylist.size() - 2));

ArrayList arraylist1 = new ArrayList();

arraylist1.add(Integer.valueOf(111));

arraylist1.add(Integer.valueOf(222));

System.out.println(arraylist1.get(arraylist1.size() - 1));

System.out.println(arraylist1.get(arraylist1.size() - 2));

}

public static void main(String args[])

{

MyClass myclass = new MyClass();

System.out.println(myclass.sum(10, 20));

myclass.test();

}

}

评论:注意它对类和初始类型的不同处理。

第二个程序的源程序:

这是一个Generic类

public class BasicGeneric <A>

{

private A data;

public BasicGeneric(A data)

{

this.data = data;

}

public A getData()

{

return data;

}

}

它的使用者:

import java.util.*;

public class MyClass2 {

public String test01(String input) {

String data01 = input;

BasicGeneric<String> basicGeneric = new BasicGeneric<String>(data01);

String data02 = basicGeneric.getData(); return data02;

}

public int test02(int input) {

Integer data01 = new Integer(input);

BasicGeneric <Integer> basicGeneric = new BasicGeneric<Integer>(data01);

Integer data02 = basicGeneric.getData();

return data02;

}

public static void main(String [] args) {

MyClass2 sample = new MyClass2();

System.out.println(sample.test01("This generic data"));

System.out.println(sample.test02(12));

}

}

各自对应的反编译程序:

public class BasicGeneric

{

public BasicGeneric(Object obj)

{

data = obj;

}

public Object getData()

{

return data;

}

private Object data;

}

import java.io.PrintStream;

public class MyClass2

{

public MyClass2()

{

}

public String test01(String s)

{

String s1 = s;

BasicGeneric basicgeneric = new BasicGeneric(s1);

String s2 = (String)basicgeneric.getData();

return s2;

}

public int test02(int i)

{

Integer integer = new Integer(i);

BasicGeneric basicgeneric = new BasicGeneric(integer);

Integer integer1 = (Integer)basicgeneric.getData();

return integer1.intValue();

}

public static void main(String args[])

{

MyClass2 myclass2 = new MyClass2();

System.out.println(myclass2.test01("This generic data"));

System.out.println(myclass2.test02(12));

}

}

评论:注意它是如何处理Template的!

这就是Java 1.5的Generic Programming,什么玩意儿啊!

全看分页树展 · 主题 跟帖


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河