2014-02-27 3 views
1

안녕하세요, 이제 우리 수업에서 객체와 클래스를 다루기 시작했습니다. 나는 클래스의 새로운 인스턴스를 만드는 것을 꽤 잘 이해한다. 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 

답변

1

이유를 두 SimpleCircle() 문을 가지고있는 사람이 당신이 double radius;를 변경 한 경우는 기술적으로 생략 할 수있는 기본 생성자가 있기 때문에 double radius = 1; 두 번째 매개 변수는 double 유형의 SimpleCircle(double)에 매개 변수를 전달하고 radius이라는 필드에 할당합니다. 둘 다 SimpleCircle(...) 문은 생성자로 알려져 있습니다. 프로그램의

샘플 출력 :

The are of the circle of radius 1.0 is 3.141592653589793 
The are of the circle of radius 25.0 is 1963.4954084936207 
The area of the circle of radius 125.0 is 49087.385212340516 
The area of the circle of radius 100.0 is 31415.926535897932 

당신은 첫 문장에서 볼 수있는, 그것은 그것이 double 때문에 1 또는 1.0과 동일한 반경을 설정하는 기본 생성자를 사용했다. 나머지는 radius 또는 두 번째 생성자에 전달 된 값을 사용합니다.

+0

과 같은 출력으로 이어질 것입니다

그래서 내가 기술적으로 ... – zeesh91

+1

@javaaaaaa하는 반경의 인스턴스를 생성 않았다 감각. 반경 자체의 인스턴스가 없습니다. – Brian

+0

왜 SimpleCircle 클래스 전체에서 반경을 사용하지 못했습니까? 왜 책은 radius = newRadius를 만들었습니까? ?? – zeesh91

0

필드 변수에 선언문의 값을 할당하지 않았기 때문에. 그렇게했다면 생성자에서 1로 설정할 필요가 없습니다. 귀하의 질문에 대한 답변이되기를 바랍니다. ? 귀하의 질문은하지 않습니다이

class SimpleCircle { 
double radius; 

// construct a circle with radius 1 
SimpleCircle() { 
    radius = 1; 

} 

class SimpleCircle { 
double radius = 1; 

// construct a circle with radius 1 
SimpleCircle() { 


}