java反射簡(jiǎn)單實(shí)例
本文實(shí)例講述了java反射簡(jiǎn)單實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
interface fruit{
public abstract void eat() ;
}
class Apple implements fruit{
public void eat() {
System.out.println("eat Apple");
}
}
class orange implements fruit{
public void eat() {
System.out.println("eat orange");
}
}
class init{
public static Properties getPro() throws FileNotFoundException, IOException{
Properties pro = new Properties() ;
File f = new File("fruit.properties") ;
if(f.exists()){
System.out.println("有配置文件!");
//從配置文件中讀取鍵值對(duì)
pro.load(new FileInputStream(f)) ;
}else{
System.out.println("沒(méi)有配置文件!");
pro.setProperty("apple", "reflect.Apple") ;
pro.setProperty("orange", "reflect.orange") ;
pro.store(new FileOutputStream(f), "FRUIT CLASS");
}
return pro ;
}
}
class Factory{
public static fruit getInstance(String className){
fruit f = null ;
try {
//通過(guò)反射得到fruit的實(shí)例對(duì)象
f = (fruit)Class.forName(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return f ;
}
}
public class Hello {
public static void main(String[] args) {
try {
Properties pro = init.getPro() ;
fruit f = Factory.getInstance(pro.getProperty("apple")) ;
if(f != null){
f.eat() ;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
結(jié)果為:
有配置文件!
eat Apple
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java?8?的異步編程利器?CompletableFuture的實(shí)例詳解
這篇文章主要介紹了Java?8?的異步編程利器?CompletableFuture?詳解,本文通過(guò)一個(gè)例子給大家介紹下Java?8??CompletableFuture異步編程的相關(guān)知識(shí),需要的朋友可以參考下2022-03-03Java數(shù)組轉(zhuǎn)換為集合的相關(guān)方法
在Java中我們經(jīng)常需要將數(shù)組從一種類(lèi)型轉(zhuǎn)換為另一種類(lèi)型,下面這篇文章主要給大家介紹了關(guān)于Java數(shù)組轉(zhuǎn)換為集合的相關(guān)方法,文中通過(guò)圖文及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Springboot報(bào)錯(cuò)java.lang.NullPointerException: null問(wèn)題
這篇文章主要介紹了Springboot報(bào)錯(cuò)java.lang.NullPointerException: null問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Springboot使用cache緩存過(guò)程代碼實(shí)例
這篇文章主要介紹了Springboot使用cache緩存過(guò)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06支付寶APP支付(IOS手機(jī)端+java后臺(tái))版
這篇文章主要為大家詳細(xì)介紹了支付寶APP支付(IOS手機(jī)端+java后臺(tái))版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05SpringBoot統(tǒng)一處理功能實(shí)現(xiàn)的全過(guò)程
最近在做項(xiàng)目時(shí)需要對(duì)異常進(jìn)行全局統(tǒng)一處理,主要是一些分類(lèi)入庫(kù)以及記錄日志等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一功能處理實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明
這篇文章主要介紹了springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java數(shù)學(xué)工具類(lèi)MathUtil詳解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)學(xué)工具類(lèi)MathUtil的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08