使用Hashtable 的子类—— Properties 完成。
import java.util.* ;
import java.io.* ;
interface Fruit{
public void grow() ;
public void eat() ;
}
class Apple implements Fruit{
public void grow(){
System.out.println("苹果在生长。。。") ;
}
public void eat(){
System.out.println("吃苹果。。。") ;
}
};
class Orange implements Fruit{
public void grow(){
System.out.println("橘子在生长。。。") ;
}
public void eat(){
System.out.println("吃橘子。。。") ;
}
};
class Banana implements Fruit{
public void grow(){
System.out.println("香蕉在生长。。。") ;
}
public void eat(){
System.out.println("吃香蕉。。。") ;
}
};
class Factory{
public static Fruit getFruit(String className){
Fruit f = null ;
try {
f = (Fruit)Class.forName(className).newInstance() ;
}catch(Exception e){}
return f ;
}
};
class PropertyOperate{
private Properties pro = null ;
public PropertyOperate(){
this.pro = new Properties() ;
this.load() ;
}
// 设置一个返回方法
public Properties getPro(){
return this.pro ;
}
// 从文件中读出属性,如果文件不存在,则创建一个默认的
private void save(){
pro.setProperty("a","Apple") ;
pro.setProperty("o","Orange") ;
// 保存在文件之中
try {
pro.storeToXML(new FileOutputStream(new File("f:\\fruit.xml")),"FRUIT FACTORY") ;
}catch(Exception e){}
}
private void load(){
File f = new File("f:\\fruit.xml") ;
if(f.exists()){
// 文件存在,则可以读取
try {
pro.loadFromXML(new FileInputStream(f)) ;
}catch(Exception e){}
}else{
// 进行创建
this.save() ;
}
}
};
class InputData{
private BufferedReader buf = null ;
public InputData(){
this.buf = new BufferedReader(new InputStreamReader(System.in)) ;
}
public String getString(){
String str = null ;
try {
str = this.buf.readLine() ;
}catch(Exception e){}
return str ;
}
};
public class Demo{
public static void main(String args[]){
Properties p = new PropertyOperate().getPro() ;
System.out.println(p) ;
System.out.print("请选择所需要的类型:") ;
String str = new InputData().getString() ;
// 进一步扩展,现在可以由用户自己输入,要使用的类型
Fruit f = Factory.getFruit(p.getProperty(str)) ;
f.grow() ;
}
};
最好在try catch 中的catch中异常信息,以便log查看.
分享到:
相关推荐
java反射技术,DaoFactory,通过反射获取接口对象,然后使用接口中的方法。
java教程ppt,讲述了java reflect.,对大家应该很有帮助
JAVA REFLECT (java 反射) 取得类的继承结构 - 类所在的包; |- public Package getPackage() - 类的声明方式; |-取得全类名:public String getName() |-只取类名: public String getSimpleName() - 类所继承...
免费共享,很详细的介绍了反射机制的原理,适合追根究底的java学习者
java 映射 说明 可以帮助大家理解java reflect
我看了看Java的反射机制,挺好的,但是JDK翻译的不怎么清楚,我自己写了两个Example,希望对大家有帮助。
NULL 博文链接:https://javaeye-hua.iteye.com/blog/1107591
Java反射的使用 这个小例子简单介绍了Java使用反射的各种方法,帮助大家快速掌握Java反射的基本使用方法。例子程序是用Android Studio编写的,如果你使用Eclipse,新建一个工程,拷贝src下面的几个类过去就可以了....
NULL 博文链接:https://lionkingzw-hotmail-com.iteye.com/blog/485923
http://blog.csdn.net/two_water/article/details/50675688 博客中的Demo
This is a tool library for Kotlin to use java reflect APIs in Kotlin simply method.It can modify or read the top level private visible property value in Kotlin way. Note: This tools only availible ...
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。...
Java基于反射机制写的动态代理操作实例。
java reflect Method getDeclaredMethod invoke getConstructor
java反射(reflect)
9.3 Java反射reflect