Think in patten of Java中的一道习题
2004-11-23 16:56
621 查看
这个问题我曾在论坛上发过,但没有解答。放在这里,给大家看看:
Think in partten of Java 中,关于Dynamic Proxies,有这么一道习题:
Exercise: Use the Java dynamic proxy to create an object that acts as a front end for a simple configuration file. For example, in good_stuff.txt you can have entries like this:
a=1
b=2
c="Hello World"
A client programmer of this NeatPropertyBundle could then write:
NeatPropertyBundle p =
new NeatPropertyBundle("good_stuff");
System.out.println(p.a);
System.out.println(p.b);
System.out.println(p.c);
The contents of the configuration file can contain anything, with any variable names. The dynamic proxy will either map to the name or tell you it doesn’t exist somehow (probably by returning null). If you set a property and it doesn’t already exist, the dynamic proxy will create the new entry. The toString( ) method should display all the current entries.
我原本想这么实现:
import java.lang.reflect.*;
import java.io.*;
class NeatPropertyBundle extends Proxy{
NeatPropertyBundle(final String textFileName){
super(new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args){
BufferedReader br=new BufferedReader(new FileReader(textFileName));
....../*这里通过method.getName获取要读的字段名,然后到配置文件里读取等等。*/
}
});
}
}
但我发现题目要求能对NeatPropertyBundle对象取任何成员变量值,而不是方法。变量名称也是任意的,这应该如何实现?
---------------------------------------
论坛上的讨论结果是,这是道错题。看看各位有何意见。
Think in partten of Java 中,关于Dynamic Proxies,有这么一道习题:
Exercise: Use the Java dynamic proxy to create an object that acts as a front end for a simple configuration file. For example, in good_stuff.txt you can have entries like this:
a=1
b=2
c="Hello World"
A client programmer of this NeatPropertyBundle could then write:
NeatPropertyBundle p =
new NeatPropertyBundle("good_stuff");
System.out.println(p.a);
System.out.println(p.b);
System.out.println(p.c);
The contents of the configuration file can contain anything, with any variable names. The dynamic proxy will either map to the name or tell you it doesn’t exist somehow (probably by returning null). If you set a property and it doesn’t already exist, the dynamic proxy will create the new entry. The toString( ) method should display all the current entries.
我原本想这么实现:
import java.lang.reflect.*;
import java.io.*;
class NeatPropertyBundle extends Proxy{
NeatPropertyBundle(final String textFileName){
super(new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args){
BufferedReader br=new BufferedReader(new FileReader(textFileName));
....../*这里通过method.getName获取要读的字段名,然后到配置文件里读取等等。*/
}
});
}
}
但我发现题目要求能对NeatPropertyBundle对象取任何成员变量值,而不是方法。变量名称也是任意的,这应该如何实现?
---------------------------------------
论坛上的讨论结果是,这是道错题。看看各位有何意见。
相关文章推荐
- KJava在移动设备中的应用
- BREW、JAVA、.NET三大平台之比较
- 【整理】JBoss研究报告
- Windows下的JAVA环境配置
- JAVA开放源码项目与工具在企业应用开发中的运用(ZZ)
- struts+hibernate使用总结
- 为了满足广大eclipse插件开发爱好者的要求特止成立eclipse插件开发群
- 多模块Struts应用程序的几个问题(及部分解决方法)
- 关于Java Servlet的Filter 技术(转)
- Struts流程
- 与低手下棋
- Java constants
- JDBC体系结构概述
- eclipse入门指南
- java1
- Java中的String str="abc"; String str=new String("abc");和String str = new String();的区别以及==与equals()的不同。
- Struts+Hibernate谈J2EE的数据表示
- 结合struts和hibernate谈J2EE架构的数据表示[转载]
- Java中的Big/Little问题
- Java之旅(9)多态性