How can I do this in Java? I can do it in Javascript? -
i'm trying larn java, sort of know javascript wondering how in java
i utilize javascript.
var player = { x: 32, y: 32, cloned: false; }; i utilize drawimage(player.x, player.y) example, how in java?
in java, statically typed, there no direct equivalent of javascript ad-hoc object presented1.
instead, 1 create [player] class representing "object" , instantiate , utilize 1 or more instances. class/instance approach, along subtyping, core of java oop model; there no escape or viable alternative in java - when using java, write java.
public class player { protected int x; protected int y; // , maybe `cloned`, etc. // maybe "action" public void move(int x, int y) { this.x = x; this.y = y; } // maybe getter/setter // (although `move` above may obsolete utilize of such setter) public int getx () { homecoming x; } public void setx (int x) { this.x = x; } // , other relevant methods .. }; and usage later might like
// create new instance of class/type player player = new player(); // manipulate , utilize player.move(19, 81); drawimage(player.getx(), player.gety()); while there "setter" provided in player class, "getter" required usage; prefer utilize actions (e.g. move), , reducing mutable state when applicable, instead of allowing direct field manipulation can increment encapsulation. in case, see how getters , setters work? wrt tangent.
also consider "programming interfaces"; real implementation have entity interface, implemented player class, understands positional movements , location. (while more tangential, wish had started programming "oop" in manner , seek eschew type contracts based on implementing class or subtype relations!)
1 true of java, not inherent of static typing; instance, c# has anonymous types (albeit static typing , inference limited within function) , different languages back upwards strongly-typed records and/or structural typing and/or dynamic typing in otherwise statically-typed language. java, however, requires such codified class back upwards @ language level.
java javascript
No comments:
Post a Comment