Can you clone an anonymous object? (Java / OOP)

情到浓时终转凉″ 提交于 2019-12-12 20:01:37

问题


I'm creating a library of very common, very simple shapes. Sphere would be a member, as well as torus, and so on. Torus has two radius fields, whereas sphere has one; that modified behavior is something we can provide by changing the implementation of functions to include both radii, and of course, adding the extra radius field. Torus is common enough that inclusion of that behavior in the permanent library is beneficial.

On the other hand, "WigglyTorus", a torus whose radii oscillate, would not be a member of the permanent library. Instead, an anonymous class is more useful; it can store the desired behavior just as well, but externally, it is treated just like an ordinary torus.

Therein lies the problem. If I want to clone a torus, my new torus can behave exactly the same. If I want to clone a WigglyTorus, the reproduced behaviors will mimic the behaviors of torus, and I don't really have a clone at all. But two uses of WigglyTorus does not justify the addition of WigglyTorus as a permanent class.

I've thought about workarounds, but none of them seem elegant. I feel like I'm missing something significant about either Java or OOP.

来源:https://stackoverflow.com/questions/19188882/can-you-clone-an-anonymous-object-java-oop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!