Think in patten of Java中的一道习题
来源:互联网 发布:淘宝怎么引进自然流量 编辑:程序博客网 时间:2024/05/18 01:28
这个问题我曾在论坛上发过,但没有解答。放在这里,给大家看看:
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 patten of Java中的一道习题
- think in java 第四章 习题答案
- Think in java中的final
- Think in Java(11)-Collections of Objects
- Java - Think in Java 第3章 习题
- Java - Think in Java 第4章 习题
- think in java第十一章持有对象 习题答案
- 一道java习题
- 一道java习题
- think in java 读书笔记
- Think in java读书笔记
- Think in java读书笔记
- Think in java读书笔记
- Think in java读书笔记
- think in java
- think in java
- Think in JAVA电子书
- Think in java读书笔记
- 希望Online -- 剑士攻略
- Palm OS系统简介
- Dynamic Dialogs学习笔记(翻译)
- KJava在移动设备中的应用
- C#中的多線程
- Think in patten of Java中的一道习题
- C#網絡編程
- MMS - 什么是MMS?
- 流程定制的小结
- 什么是WAP?
- 使用Ado.net获取数据库架构信息
- 軟件測試方法
- Understanding and Using Assemblies and Namespaces in .NET
- 程序员的爱情之数字表白