public class Rectangle {
int top;
int left;
int width;
int height;
public Rectangle(int top, int left, int width, int height) {
this.top = top;
this.left = left;
this.width = width;
this.height = height;
}
public Rectangle(int top, int left, int width) {
this.top = top;
this.left = left;
this.width = width;
}
public Rectangle(int top, int left) {
this.top = top;
this.left = left;
this.width = 0;
this.height = 0;
}
public Rectangle(int top) {
this.top = top;
}
public static void main(String[] args) {
Rectangle original = new Rectangle (1, 1, 5, 5);
Rectangle copy = new Rectangle (original);
}
}
Tim Prozorov
8 уровень
Rebjat podskazhite pochemu copiju objekta nemogu sdelat. > Rectangle copy = new Rectangle (original);
Обсуждается
Комментарии (8)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
--------Master
26 сентября 2018, 21:46
А где у тебя объявлен и реализован конструктор, принимающий параметром объект Rectangle?
0
Tim Prozorov
26 сентября 2018, 21:56
Spasibo , sdelal > pravelno ? i ewjo kogda zapuskaju to poluchaju sledushee > Rectangle@4554617c
Rectangle@74a14482
public Rectangle(Rectangle rectangle)
{
this.left = left;
this.top = top;
this.height = height;
this.width = width;
}
public static void main(String[] args) {
Rectangle original = new Rectangle (1, 1, 5, 5);
Rectangle copy = new Rectangle (original);
System.out.println(original);
System.out.println(copy);
}
}
0
--------Master
26 сентября 2018, 22:00
В этой строке ты, по сути, присваиваешь переменной ее же собственное значение, т.е. left = left (this - текущий объект). А объект rectangle, передаваемый в конструктор, никак не используется
0
Tim Prozorov
26 сентября 2018, 22:23
a vsjo ponjal , toka neponjal pochemu nevqvoditsja normalno > mesto etogo vqdajot >Rectangle@4554617c
Rectangle@74a14482
public Rectangle(Rectangle rectangle)
{
this.left = rectangle.left;
this.top = rectangle.top;
this.height = rectangle.height;
this.width = rectangle.width;
}
public static void main(String[] args) {
Rectangle original = new Rectangle (1, 1, 5, 5);
Rectangle copy = new Rectangle (original);
System.out.println(original);
System.out.println(copy);
}
}
0
--------Master
26 сентября 2018, 22:28
Нормально выводится, в классе Rectangle метод toString не переопределен - вызывается метод класса Object.
При этом original и copy ссылаются на разные объекты, хоть и с одинаковыми значениями полей
0
Tim Prozorov
26 сентября 2018, 22:30
spasibo ogromnoe za vremja > i poslednqi vopros >> Rectangle метод toString не переопределен - вызывается метод класса Object. > kak eto ispravit
0
--------Master
26 сентября 2018, 22:35
Объявить и реализовать метод, возвращающий строковое представление объекта)) Это еще вроде будет проходиться или можешь в инете почитать. В Идее через alt+insert можно сгенерировать стандартный метод, посмотреть
0
Tim Prozorov
26 сентября 2018, 22:42
objazatelno, spasibo
0