反射例子(配置文件)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                反射例子(配置文件)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                配置文件
className = fanshe.Student1 showInfo = show1類student1
public class Student1 {public Student1() {System.out.println("調(diào)用了Student1 無參構(gòu)造函數(shù)");}public void show1(){System.out.println("調(diào)用了show1()方法");} }測試類
public class Test {public static void main(String[] args) throws Exception{ //1. 獲取類Class clazz = Class.forName(getValue("className"));//2. 獲取showinfo方法Method m_showInfo = clazz.getMethod(getValue("showInfo"));//3.調(diào)用showInfo()方法//3.1 實(shí)例化一個類對象,invoke中必須傳入類對象的實(shí)例Object obj = clazz.getConstructor().newInstance();//3.2 用實(shí)例化好的類對象去調(diào)用方法m_showInfo.invoke(obj);} public static String getValue(String key) throws Exception{Properties pro = new Properties();//獲取配置文件對象 FileReader file = new FileReader("proInfo.txt");//獲取輸入流對象pro.load(file);//加載file.close();//關(guān)閉流對象return pro.getProperty(key);//返回需要取的目標(biāo)值} }
輸出
調(diào)用了Student1 無參構(gòu)造函數(shù) 調(diào)用了show1()方法
轉(zhuǎn)載于:https://www.cnblogs.com/zzh-blog/p/9690756.html
總結(jié)
以上是生活随笔為你收集整理的反射例子(配置文件)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Linux自动化之Cobbler补鞋匠安
- 下一篇: 如何把网址配置为http和https可以
