反射机制(1)
改反射把所有的代码都贴上去
首先写3个java类已经一个接口
package com.hp.test;
public interface Office {
public void print();
}
package com.hp.test;
public class Excel implements Office {
public void print(){
System.out.println("这是excal");
}
}
package com.hp.test;
public class ppt {
public void print(String name ,String time){
System.out.println(name+"ppt开始执行----"+time+"ppt执行结束");
}
}
package com.hp.test;
public class Word implements Office{
public void print(){
System.out.println("word开始执行");
}
}
第一个classtest.java类
package com.hp.test;
public class ClassTest {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
// 创建TOO类的对象
Too too = new Too();
//这个too类本身又是谁的对象
//所有的类本身都属于claas类的对象(类类型)
//如何创建一个类的类类型
//通过class方法,,每个类都有一个隐藏的静态成员变量class
Class c1 = Too.class;
//通过class.formName
Class c2 = Class.forName("com.hp.test.Too");
//通过类对象的getclass()方法来获取类的类类型
Class c3 = too.getClass();
System.out.println(c1=c2);
System.out.println(c2=c3);
//能不能通过类的类类型,能够获得一个类的对象
Too too2 = (Too) c1.newInstance();
}
}
class Too{
public void print(){
System.out.println("too执行");
}
}
第二个javatest2.java类
package com.hp.test;
public class ClassTest2 {
public void print(String office){
if(office.equals("Word")){
Word word = new Word();
word.print();
}
if(office.equals("Excel")){
Excel excel = new Excel();
excel.print();
}
}
public static void main(String[] args) {
ClassTest2 ct2 = new ClassTest2();
ct2.print("Excel");
ct2.print("Word");
}
}
第三个ClassTest3.java类
package com.hp.test;
public class ClassTest3 {
// 反射实现运行时加载,不需要一次性全部加载完成所以的类的事例
public void print(String office) throws ClassNotFoundException,
InstantiationException, IllegalAccessException {
if (office.equals("Word")) {
Class c1 = Class.forName("com.hp.test." + office);
Office off = (Office) c1.newInstance();
off.print();
}
if (office.equals("Excel")) {
Class c1 = Class.forName("com.hp.test." + office);
Office off = (Office) c1.newInstance();
off.print();
}
}
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException {
ClassTest3 ct3 = new ClassTest3();
ct3.print("Word");
ct3.print("Excel");
}
}
第四个ClassTest4.java
package com.hp.test;
public class ClassTest4 {
public void print() {
Class c1 = int.class;
Class c2 = String.class;
Class c3 = Double.class;
Class c4 = double.class;
Class c5 = void.class;
//获取反射类的类类型的原始名字
System.out.println(c1.getName());
System.out.println(c2.getName());
System.out.println(c3.getName());
System.out.println(c4.getName());
System.out.println(c5.getName());
System.out.println(c2.getSimpleName());
}
public static void main(String[] args) {
ClassTest4 ct4 = new ClassTest4();
ct4.print();
}
}
第五个ClassTest5.java
package com.hp.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ClassTest5 {
public void print() throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
//获得word类的类型
Class c1 = Word.class;
Word word = (Word) c1.newInstance();
Method method =c1.getMethod("print");
method.invoke(word);
//获得ppt类的类型
Class c2 = ppt.class;
Object obj = c2.newInstance();
Method me = c2.getMethod("print", new Class[]{String.class,String.class});
me.invoke(obj,new Object[]{"2009","2010年"});
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
ClassTest5 ct5 = new ClassTest5();
ct5.print();
}
}