反射机制(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();
}
}
?
轉載于:https://www.cnblogs.com/lyh-8/p/6085728.html
總結
- 上一篇: 人们在购车时考虑哪些因素最多?
- 下一篇: 查询电脑信息