Какой смысл void setradius в конце этого кода? [дублировать]JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Какой смысл void setradius в конце этого кода? [дублировать]

Сообщение Anonymous »

Я только начал свой второй курс Java в колледже, и я впервые вижу это. Какой смысл void setradius в конце кода. Код работает, если я удалю его, я просто не уверен, в чем смысл включить его в конструктор. Ниже приведен код с урока.public class TestCircle {
/** Main method */
public static void main(String[] args) {
// Create a circle with radius 1
Circle circle1 = new Circle();
System.out.println("The area of the circle of radius "
+ circle1.radius + " is " + circle1.getArea());

// Create a circle with radius 25
Circle circle2 = new Circle(25);
System.out.println("The area of the circle of radius "
+ circle2.radius + " is " + circle2.getArea());

// Create a circle with radius 125
Circle circle3 = new Circle(125);
System.out.println("The area of the circle of radius "
+ circle3.radius + " is " + circle3.getArea());

// Modify circle radius
circle2.radius = 100; // or circle2.setRadius(100)
System.out.println("The area of the circle of radius "
+ circle2.radius + " is " + circle2.getArea());
}
}

// Define the circle class with two constructors
class Circle {
double radius;

/** Construct a circle with radius 1 */
Circle() {
radius = 1;
}

/** Construct a circle with a specified radius */
Circle(double newRadius) {
radius = newRadius;
}

/** Return the area of this circle */
double getArea() {
return radius * radius * Math.PI;
}

/** Return the perimeter of this circle */
double getPerimeter() {
return 2 * radius * Math.PI;
}

/** Set a new radius for this circle */
void setRadius(double newRadius) {
radius = newRadius;
}
}
< /code>
Я смотрел пару уроков на YouTube о конструкторах в Java. Я не могу найти ответ через Google, и при этом я не понимаю, какой вопрос я должен задать.

Подробнее здесь: https://stackoverflow.com/questions/793 ... -this-code
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»