Как в java массиве заменить координаты фигур?
public class Main {
private static int x;
private static int y;
public static <figures> void main(String[] args) {
Square square = new Square(5,7);
Circle circle = new Circle(2,5);
System.out.println("Старые координаты " + square + " : " + " " + " x = " + square.x + " " + " y = " + square.y);
System.out.println("Старые координаты " + circle + " : " + " " + " x = " + circle.x + " " + " y = " + circle.y);
Figure[] figures = new Figure[2];
figures[0] = square;
figures[1] = circle;
for (int i = 0; i < figures.length; i++) {
figures[i] = setX(8);
figures[i] = setY(9);
};
System.out.println("Новые координаты " + square + " : " + " " + " x = " + square.x + " " + " y = " + square.y);
System.out.println("Новые координаты " + circle + " : " + " " + " x = " + circle.x + " " + " y = " + circle.y);
}
public static Figure setX(int x) {
Main.x = x;
return null;
}
public static Figure setY(int y) {
Main.y = y;
return null;
}
}
public abstract class Figure {
protected int x;
protected int y;
public Figure(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) {
this.x=x;
}
public void setY(int y) {
this.y=y;
}
}
Ответы (3 шт):
Автор решения: Fleckinger
→ Ссылка
Вам IDE пишет в чем проблема, что в мейне нет такого метода, так как вам нужно вызывать этот метод у объекта Figure. Например
Figure[] figures = new Figure[2];
figures[0] = square;
figures[1] = circle;
for (int i = 0; i < figures.length; i++) {
figures[i].setX(8);
figures[i].setY(9);
};
И унаследовать Square и Circle от Figure
abstract class Figure {
protected int x;
protected int y;
public Figure(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) {
this.x=x;
}
public void setY(int y) {
this.y=y;
}
}
class Square extends Figure {
public Square(int x, int y) {
super(x, y);
}
}
class Circle extends Figure {
public Circle(int x, int y) {
super(x, y);
}
}
Автор решения: Andrey Likhtarovich
→ Ссылка
вы меняете не те переменные . Вы меняете статические переменные в классе Main, вам следует менять их, обращаясь к фигуре.
Автор решения: and73
→ Ссылка
Получилось и работает как надо.
public class Main {
public static void main(String[] args) {
Square square = new Square(5,7);
Circle circle = new Circle(2,5);
System.out.println("Старые координаты " + square + " : " + " " + " x = " + square.x + " " + " y = " + square.y);
System.out.println("Старые координаты " + circle + " : " + " " + " x = " + circle.x + " " + " y = " + circle.y);
Figure[] figures = new Figure[2];
figures[0] = square;
figures[1] = circle;
for (Figure figure : figures) {
figures[0].setX(square.movex());
figures[0].setY(square.movey());
figures[1].setX(circle.movex());
figures[1].setY(circle.movey());
}
System.out.println("Новые координаты " + square + " : " + " " + " x = " + square.x + " " + " y = " + square.y);
System.out.println("Новые координаты " + circle + " : " + " " + " x = " + circle.x + " " + " y = " + circle.y);
}
}
abstract class Figure {
protected int x;
protected int y;
public Figure(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
public interface Moveable {
int movex();
int movey();
}