package javapack.util;
import java.lang.reflect.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class testref {
public void testref(){System.out.println("testref()");}
public void testref(String s){System.out.println("testref(String)");}
public void testref(int i, double d){System.out.println("testref(int,double)");}
public void testref(String s , double d){System.out.println("testref(String,double)");}
public void testref(String s , long l){System.out.println("testref(String,long)");} public void test1(){System.out.println("testref: test1()");}
public void test2(){System.out.println("testref: test2()");}
public void test3(){System.out.println("testref: test3()");}
public void test4(){System.out.println("testref: test4()");}
public void test5(){System.out.println("testref: test5()");}
public void test6(){System.out.println("testref: test6()");}
}
public class testclass {
public static void main(String[] args){
try{
Class cc = testref.class ;
System.out.println("this class : "+cc.toString());
// Method[] m = cc.getMethods();
// for(int i=0 ; i< m.length ; i++)
// { System.out.println("Method["+i+"]: "+m[i].toString());
// }
// Constructor[] ctr = cc.getConstructors();
// for(int i=0 ; i< ctr.length ; i++)
// { System.out.println("Constructor["+i+"]: "+ctr[i].toString());
// }
Constructor cp = cc.getConstructor(new Class[]{String.class});
testref tt =(testref)cp.newInstance(new Object[] {"this"});
tt.test1();
}catch(Exception ex) {
System.out.println("can't create!");
}
}
} 说明:我主要是用来测试REFLECT机制的问题,想用该机制来取得某类的某种构构函数,以便用来生成各种对像。但是却不能生成,不知道是何原因,请高手给予解答!分值不够还可以加
import java.lang.reflect.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class testref {
public void testref(){System.out.println("testref()");}
public void testref(String s){System.out.println("testref(String)");}
public void testref(int i, double d){System.out.println("testref(int,double)");}
public void testref(String s , double d){System.out.println("testref(String,double)");}
public void testref(String s , long l){System.out.println("testref(String,long)");} public void test1(){System.out.println("testref: test1()");}
public void test2(){System.out.println("testref: test2()");}
public void test3(){System.out.println("testref: test3()");}
public void test4(){System.out.println("testref: test4()");}
public void test5(){System.out.println("testref: test5()");}
public void test6(){System.out.println("testref: test6()");}
}
public class testclass {
public static void main(String[] args){
try{
Class cc = testref.class ;
System.out.println("this class : "+cc.toString());
// Method[] m = cc.getMethods();
// for(int i=0 ; i< m.length ; i++)
// { System.out.println("Method["+i+"]: "+m[i].toString());
// }
// Constructor[] ctr = cc.getConstructors();
// for(int i=0 ; i< ctr.length ; i++)
// { System.out.println("Constructor["+i+"]: "+ctr[i].toString());
// }
Constructor cp = cc.getConstructor(new Class[]{String.class});
testref tt =(testref)cp.newInstance(new Object[] {"this"});
tt.test1();
}catch(Exception ex) {
System.out.println("can't create!");
}
}
} 说明:我主要是用来测试REFLECT机制的问题,想用该机制来取得某类的某种构构函数,以便用来生成各种对像。但是却不能生成,不知道是何原因,请高手给予解答!分值不够还可以加
你多了一个 void