欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java反射簡(jiǎn)單實(shí)例

 更新時(shí)間:2015年02月03日 11:53:37   作者:Benjamin_whx  
這篇文章主要介紹了java反射機(jī)制,以一個(gè)簡(jiǎn)單實(shí)例形式分析了Java反射的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了java反射簡(jiǎn)單實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

復(fù)制代碼 代碼如下:
package reflect;

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的實(shí)例詳解

    這篇文章主要介紹了Java?8?的異步編程利器?CompletableFuture?詳解,本文通過(guò)一個(gè)例子給大家介紹下Java?8??CompletableFuture異步編程的相關(guān)知識(shí),需要的朋友可以參考下
    2022-03-03
  • Java數(shù)組轉(zhuǎn)換為集合的相關(guān)方法

    Java數(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-01
  • Springboot報(bào)錯(cuò)java.lang.NullPointerException: null問(wèn)題

    Springboot報(bào)錯(cuò)java.lang.NullPointerException: null問(wèn)題

    這篇文章主要介紹了Springboot報(bào)錯(cuò)java.lang.NullPointerException: null問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Springboot使用cache緩存過(guò)程代碼實(shí)例

    Springboot使用cache緩存過(guò)程代碼實(shí)例

    這篇文章主要介紹了Springboot使用cache緩存過(guò)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java ArrayList中的remove方法介紹

    java ArrayList中的remove方法介紹

    大家好,本篇文章主要講的是java ArrayList中的remove方法介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下
    2022-01-01
  • 支付寶APP支付(IOS手機(jī)端+java后臺(tái))版

    支付寶APP支付(IOS手機(jī)端+java后臺(tái))版

    這篇文章主要為大家詳細(xì)介紹了支付寶APP支付(IOS手機(jī)端+java后臺(tái))版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • SpringBoot統(tǒng)一處理功能實(shí)現(xiàn)的全過(guò)程

    SpringBoot統(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-03
  • springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明

    springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明

    這篇文章主要介紹了springboot集成mybatis?plus和dynamic-datasource注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java數(shù)學(xué)工具類(lèi)MathUtil詳解

    Java數(shù)學(xué)工具類(lèi)MathUtil詳解

    這篇文章主要為大家詳細(xì)介紹了Java數(shù)學(xué)工具類(lèi)MathUtil的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • java轉(zhuǎn)化為exe程序步驟詳解

    java轉(zhuǎn)化為exe程序步驟詳解

    在本篇內(nèi)容里我們給大家分享了關(guān)于java轉(zhuǎn)化為exe程序的具體步驟和相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-03-03

最新評(píng)論