主题:【评论】Sun发布了最新的Java版本-- 5.0 Tiger版 -- Highway
今天忽然有兴趣,写了两个很简单的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,什么玩意儿啊!
- 相关回复 上下关系8
Java 1.5的Boxing/UnBoxing是如何实现的 老兵帅客 字1319 2004-07-01 19:53:47
Java 也support template了? 无斋主人 字24 2004-07-02 16:29:33
Sun将在Java 1.5或者JDK 5.0中提供此Feature 老兵帅客 字418 2004-07-02 18:37:03
Java 1.5的Generic是如何实现的
😮老兵先不忙下结论。也许是DJ的问题呢? Highway 字141 2004-07-01 18:17:24
回复 老兵帅客 字311 2004-07-01 19:47:18
😁我修改了一下你的程序,比如: Highway 字490 2004-07-01 21:52:45
对,就是那么回事 老兵帅客 字754 2004-07-02 05:37:14