안녕하세요, 이제 우리 수업에서 객체와 클래스를 다루기 시작했습니다. 나는 클래스의 새로운 인스턴스를 만드는 것을 꽤 잘 이해한다. Howerver, 그것의 데이터 필드 및 방법은 문제가 이해하는 데있어. 여기에 제가 쓴 책의 샘플이 있습니다. "나는 반경이 1 인 원을 만들었습니다. 왜냐하면 BC가되는 이유는 SimpleCircle 클래스에서 이중 반지름을 이미 선언했기 때문에 SimpleCircle을 다시 작성해야하는 이유는 무엇입니까? 이 프로그램의 뮤 테이터하지만이 모든에 좀 더 간단한 설명을하시기 바랍니다 싶습니다.Java OBJECTS and Classes beginner
public class TestSimpleCircle {
public static void main(String[] args) {
SimpleCircle circle1 = new SimpleCircle();
System.out.println("The area of the circle of radius " + circle1.radius + " is " + circle1.getArea());
// create a circle with radius 25
SimpleCircle circle2 = new SimpleCircle(25);
System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());
// create a circle with radius 125
SimpleCircle circle3 = new SimpleCircle(125);
System.out.println("The area of the circle of radius " + circle3.radius + " is " + circle3.getArea());
// Modify circle radius of second object
circle2.setRadius(100); //or circle2.setRadius(100);
System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());
} // end main
} // end class
class SimpleCircle {
double radius;
// construct a circle with radius 1
SimpleCircle() {
radius = 1;
}
// construct a circle with a specified radius
SimpleCircle(double newRadius) {
radius = newRadius;
}
// return the are 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;
}
} // end class
과 같은 출력으로 이어질 것입니다
그래서 내가 기술적으로 ... – zeesh91
@javaaaaaa하는 반경의 인스턴스를 생성 않았다 감각. 반경 자체의 인스턴스가 없습니다. – Brian
왜 SimpleCircle 클래스 전체에서 반경을 사용하지 못했습니까? 왜 책은 radius = newRadius를 만들었습니까? ?? – zeesh91