Initialization of structures with a constructor in Java
Size size = new Size(10, 10);
Point point = new Point(5, 5);
Rectangle rect = new Rectangle(size, point);
//The Java language has no structures
class Size {
public int width, height;
public Size (int width, int height) {
this.width = width;
this.height = height;
}
}
class Point {
public int top, left;
public Point (int top, int left) {
this.top = top;
this.left = left;
}
}
class Rectangle {
public Size size;
public Point point;
public Rectangle (Size size, Point point) {
this.size = size;
this.point = point;
}
}
- Log in to post comments