I'm doing some processing of images and in my work I use the package java.awt.geom
. I am use the class Point
. This class extends from Point2D
and inherits get methods that return double. Point
is meant to be an integer representation of a point.
To access the integer x
and y
values in Point
you need to use publicly accessible variable x
and y
; My question is
1) Isn't it bad practice to allow public access to an instance variable? eg. This question
2) Is there a better design of this?
Yes it is bad practice (at least by today's standards), but there are a few reasons for this...
Point2D
needs to be as generic as possible In detail...
Point2D
is a generic object used to represent any kind of point that can be represented in a 2D space. In order to have maximum use as a generic class, the point needs to allow for the highest level of precision possible (ie a double
). This is evident by the getX()
and getY()
methods that return double
s. By using double
s, it allows any sub-class to be compatible with these methods, as a double is the largest primitive java number type with precision, so any sub-class that uses any other primitive numerical variable will always be able to return it as a double
without losing any precision. Point
as being a simplified version of Point2D
, that doesn't maintain any precision - however it must still conform to the abstract method declarations of the super-class Point2D
. An int
can be cast into a double
directly by the JVM without losing any precision - if Point2D
were something other than a double
, you would lose some of the precision during the cast. (For example, you couldn't cast it to a float
, as larger int
values wouldn't be able to be directly represented as a float
without chopping off some of the higher numbers) File.separator
Point
is a class that is used in a lot of places, and itself is still a pretty generic object. It is much easier/simpler/quicker for programmers to reference an int
directly then it is to cast every time you want to get the value (int)getX()
. It is also quicker to process by the JVM, as it doesn't need to convert an int
to a double
, and then back to an int
again. If you want a better design, you would probably be best to create additional methods for accessing the int
values as int
s - ie create the methods getXInt()
and getYInt()
, and then change the variables back to being non-public (provided you don't need to retain backwards compatibility).
1) Yes it is bad practise. The Point was introduced in JDK 1.0, Java always tries to keep backward compatibility that's why this weren't changed in the past I think.
There are getters for x
and y
. They are returning a double because of the abstract method declaration in Point2D
. This isn't nice but can safely casted to an int
as the value of the Point is returned which actually is an int
.
2) If you need to deal with AWT you should simply accept this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.