java - Is it acceptable to rewrite methods in a dependent class to avoid get().get().get() code? -
I used to brush my own points (int x, int y) and circle (point center, int radius) sections Was written for class relations and some basic mathematics.
If I want to draw a circle on the screen, then my current code looks like:
g.draw oval (circle.getCenter (.getX) - circle.getRadius (), circle.getCenter (). GetY () - circle.getRadius (), circle.getRadius () * 2, circle.getRadius () * 2);
Is it acceptable to write methods in my circle class which passes the fields of my point in my main square / applet?
g.draw oval Circle.getX () - circle.getRadius (), circle.getY () - circle.getRadius (), circle.getRadius () * 2, circle.getRadius ( ) * 2);
My concern is that, when I start building more concrete objects (such as a player class), then I will terminate the code where a field through the layers The amount of text can be passed from
int x = player.getCircle (). Getpoint () GetX (); Instead of
int x = player.getX ();
It seems that the beauty of Java is writing classes once you have whatever you need, and let them handle all the upper part. It is useful for me to know how other people handle such content, so I try to find many etiquettes, formatting and standard guidelines.
I want my code to be transparent, as it will immediately be clear to convert it to another sign that the last line of code is getting an integer through a series of dependencies?
This question can get many different opinions, which is right for me - but I think that there can be a special standard which is popular in most Java codes.
You can always define ways to access properties of internal objects:
square circle {int getX () {int x = -1; If (getCenter ()! = Null) x = getCenter () GetX (); Return x; }} Class player {int getX () {int x = -1; If (getCircle ()! = Null) x = getCircle () GetX (); Return x; }}
The introduction of these methods is that the code using external code (the player in your example) should not put all the blank check, because the player Getcircle (). GetPoint () Getx ()
should be replaced with this secure code, to avoid NullPointerExceptions:
if (player! = Zero) if player (.getCircle ()! =) If (player.) Tactical (). GetPoint ()! = Faucet) X = Player. Trakrik (). Getpoint () GetX ();
Of course, this ugly code can also be avoided by using the Java 8 Optional.
Comments
Post a Comment