简体   繁体   English

Java类设计问题

[英]Java class design issue

Here is a class design: http://pastebin.com/1RSdmtXi 这是一个类设计: http//pastebin.com/1RSdmtXi

If i put only A to class Expect I would like to see only A's getters and setters, or vica vera to B. If put in A and B , then all of getters and setters should be visible. 如果我只把A放到课堂上Expect我想只看到A的吸气剂和制定者,或者只看到B的维卡。如果放入AB ,那么所有的吸气剂和制定者都应该是可见的。

So an example for only A : 因此,对于只是一个例子A

A[] aExampe = {new A("Tim",1)};
Expect exp = new Expect(aExampe);

exp.getA(); --> visible
exp.getB(); --> not visible

You may advice another design for this. 您可以为此建议另一种设计。

Try using this: 试试这个:

 Expect.class.getMethod("getA", null).setAccessible(true);
 Expect.class.getMethod("getB", null).setAccessible(false);

But I suggest you to change your architecture. 但我建议你改变你的架构。

Why this is a need? 为什么需要这个?

If you change the class Expect (by adding an removing fields), you can add and remove getters and setters too. 如果更改Expect类(通过添加删除字段),则还可以添加和删除getter和setter。

If you want to change the class without modifying and recompiling client classes, define an interface containing all needed methods, implement it by Expect class, and instead of removing the methods, just make them empty methods (without any body). 如果要在不修改和重新编译客户端类的情况下更改类,请定义包含所有所需方法的接口,通过Expect类实现它,而不是删除方法,只需将它们设为空方法(没有任何主体)。

By this pattern, you will not use compile time checking, and won't need to use dirty reflection for normal method calls. 通过这种模式,您将不会使用编译时检查,并且不需要使用脏反射进行常规方法调用。

In the class Expect you should only have getA(), setA, getB and setB, not all the getters and setters for A and B's attributes. 在Expect类中,你应该只有getA(),setA,getB和setB,而不是A和B属性的所有getter和setter。 Those ones belong to those classes. 那些属于那些类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM