开发者

Why does Point allow you to access x and y directly?

Both x and y and integers, while getX() and getY() returns a doubles.

Why aren't开发者_如何转开发 there e.g. getXIntValue(), getXDoubleValue() and vice versa for y?


Note the @since in the Javadoc -- 1.0

There is broad consensus that Point was very poorly designed.


Point extends Point2D and Point2D has getX() and getY() defined as returning double. As @Dilum mentions above, its bad design from the stone ages of java.


As others have said, it's because it's ancient. But I like it because it saves typing and adds clarity; IMHO getters and setters can be a waste of time for simple classes if they're public and all they're going to do is to return the value and set it, which is all you want 99% of the time.

Design philosophy changed and now everything uses them. But things are coming full circle and in a modern languages like Groovy these would be "properties" with getters / setters inferred and syntax pretty similar to that for accessing public fields in Java.

E.g. http://groovy.codehaus.org/Groovy+Beans

Edit: and according to an (unofficial) Scala style guide,

Note that fields may actually be used in a number of situations where accessors and mutators would be required in languages like Java. Always prefer fields over methods when given the choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜