也可以将不想实例化的类定义成抽象的,构造方法依旧可以定义,还可以定义其它的静态方法,在这个静态方法里即使是实现当前抽象类也是可以的。 到具体应用还需要改进完善,只是提个思路。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Class clazz = Student.class;
System.out.println(clazz.getName());
Method d = null;
try {
d = clazz.getMethod("main",String[].class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(d);
try {
d.invoke(null,(Object)null);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
abstract class Student extends Person {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Student.name);
new Student(){
void print(){
System.out.println("hehe");
}
}.print();
}
abstract void print();
}
class Person{
static String name = "ffdd ";
void shut(){
}
}
|