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 ответа
Вам 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);
}
}
-
исправил, ошибки исчезли, однако координаты остаются старыми, не изменяются.– and7331 окт 2021 в 21:13
-
Не знаю как у вас унаследованы классы Square и Circle, однако у меня нормально изменяются поля. Старые координаты Square@2ef9b8bc : x = 5 y = 7 Старые координаты Circle@5d624da6 : x = 2 y = 5 Новые координаты Square@2ef9b8bc : x = 8 y = 9 Новые координаты Circle@5d624da6 : x = 8 y = 9 31 окт 2021 в 21:35
-
@Fleckinger, пока что там реализованы статические методы в классе
Main
, которые меняют статические же поля в классеMain
. Нельзя просто так взять и скопировать рабочий код. 31 окт 2021 в 21:37 -
@and73 а зачем вам вообще поля и статические методы в классе Main если вы работаете с объектами наследниками Figure. Отредактировал ответ, дополнил двумя классами унаследованными от Figure 31 окт 2021 в 21:40
-
1@Alex Rudenko Голоса от участников с репутацией менее 15 учитываются, но не меняют публично отображаемое значение рейтинга.– and732 ноя 2021 в 9:51
вы меняете не те переменные . Вы меняете статические переменные в классе Main, вам следует менять их, обращаясь к фигуре.
Получилось и работает как надо.
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();
}
Figure
, вызывая статические методы классаMain
, которые меняют статические же поля в классеMain
? В ответе показан работающий код