Accessing inner anonymous class members

后端 未结 7 1904
灰色年华
灰色年华 2021-01-05 18:00

Is there any way other than using reflection to access the members of a anonymous inner class?

7条回答
  •  既然无缘
    2021-01-05 18:13

    In the case of anonymous classes, there is also a tradeoff between the clutter caused by the class and the convenience of having it anonymous. Complicated classes rarely belong as anonymous but rather as named private inner.

    In most anonymous classes, we only need to "feed" knowledge and can do it at construction. In a few anonymous classes (e.g., return value vehicles) we also care about one return value.

    As we know, data members should not directly be accessed but rather with a getter setter. This, if you find yourself in a situation that you have added lots of getters and setters, you are probably doing something wrong anyway and shouldn't be using an anonymous class.

提交回复
热议问题