万物皆对象
类是java.lang.Class的对象
package com.mysec.reflex;
import java.util.Scanner;
//任何一个类都是Class的实例对象
public class OfficeBeffer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
methodStart(name);
}
public static void methodStart(String name){
try {
//知道该类的对象通过getClass方法
// test c1 = new test();
// c1.getClass();
//任何一个类都有一个隐含的静态成员变量
// Class<?> c2 = test.class;
//动态加载类,在运行时刻加载
//com.mysec.reflex.Word
Class<?> c = Class.forName(name);
//通过类类型,创建该类的对象
OfficeAble oa = (OfficeAble)c.newInstance();
oa.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.mysec.reflex;
public interface OfficeAble {
public void start();
}
package com.mysec.reflex;
public class Word implements OfficeAble{
@Override
public void start() {
System.out.println("wordStarts...");
}
}