Structures methods in Java
Forums:
//In Java there are no structures
class Point {
public int x, y;
public String toText() {
return String.format("x = %d; y = %d", x, y);
}
public void move(int right, int down) {
x += right;
y += down;
}
}
Point p1 = new Point();
p1.x = 1;
p1.y = 2;
String str1 = p1.toText();
//str1 is "x = 1; y = 2"
p1.move(5, -1);
String str2 = p1.toText();
//str2 is "x = 6; y = 1"
- Log in to post comments