2013-07-09 8 views
2

두 개의 서브 클래스 (CircleFromSimpleGeometricObject 및 RectangleFromSimpleGeometricObject)로 확장 된 수퍼 클래스 (SimpleGeometricObject)와 TestCircleRectangle이라는 CircleFromSimpleGeometricObject 및 RectangleFromSimpleGeometricObject를 호출하는 클래스가 있습니다.자바 수퍼 클래스 상속

/** Construct a default geometric object */ 
public SimpleGeometricObject() { 
    dateCreated = new java.util.Date(); 
} 

나는 누군가가 나를 도울 수, 이런 이유와 방법에 대해 조금 혼란 스러워요 :

public CircleFromSimpleGeometricObject(double radius){ 
    this.radius = radius; 
} 

어떻게 든 슈퍼 클래스 SimpleGeometricObject를 호출 서브 클래스 CircleFromSumpleGeometricObject, 이 코드 라인, 디버거를 다음 이것이 일어나는 이유를 이해 하는가? 아래는 모든 수업의 코드입니다.

public class SimpleGeometricObject { 
private String color = "white"; 
private boolean filled; 
private java.util.Date dateCreated; 

/** Construct a default geometric object */ 
public SimpleGeometricObject() { 
    dateCreated = new java.util.Date(); 
} 

/** Construct a geometric object with the specified color 
* and filled value */ 
public SimpleGeometricObject(String color, boolean filled) { 
    dateCreated = new java.util.Date(); 
    this.color = color; 
    this.filled = filled; 
} 

/** Return color */ 
public String getColor() { 
    return color; 
} 

/** Set a new color */ 
public void setColor(String color) { 
    this.color = color; 
} 

/** Return filled. Since filled is boolean, 
    its get method is named isFilled */ 
public boolean isFilled() { 
    return filled; 
} 

/** Set a new filled */ 
public void setFilled(boolean filled) { 
    this.filled = filled; 
} 

/** Get dateCreated */ 
public java.util.Date getDateCreated() { 
    return dateCreated; 
} 

/** Return a string representation of this object */ 
public String toString() { 
    return "created on " + dateCreated + "\ncolor: " + color + 
     " and filled: " + filled; 
} 
} 


public class CircleFromSimpleGeometricObject 
extends SimpleGeometricObject { 
private double radius; 

public CircleFromSimpleGeometricObject() { 

} 

public CircleFromSimpleGeometricObject(double radius){ 
    this.radius = radius; 
} 

public CircleFromSimpleGeometricObject(double radius, 
     String color, boolean filled) { 
    this.radius = radius; 
    setColor(color); 
    setFilled(filled); 
} 

/** Return radius */ 
public double getRadius() { 
    return radius; 
} 

/** Set a new radius */ 
public void setRadius(double radius) { 
    this.radius = radius; 
} 

/** Return area */ 
public double getArea() { 
    return radius * radius * Math.PI; 
} 

/** Return diameter */ 
public double getDiameter() { 
    return 2 * radius; 
} 

/** Return perimeter */ 
public double getPerimeter() { 
    return 2 * radius * Math.PI; 
} 

/** Print the circle info */ 
public void printCircle() { 
    System.out.println("The circle is created " + getDateCreated() + 
      " and the radius is " + radius); 
} 
} 



public class RectangleFromSimpleGeometricObject 
extends SimpleGeometricObject { 
private double width; 
private double height; 

public RectangleFromSimpleGeometricObject() { 
} 

public RectangleFromSimpleGeometricObject(
     double width, double height) { 
    this.width = width; 
    this.height = height; 
} 

public RectangleFromSimpleGeometricObject(
     double width, double height, String color, boolean filled) { 
    this.width = width; 
    this.height = height; 
    setColor(color); 
    setFilled(filled); 
} 

/** Return width */ 
public double getWidth() { 
    return width; 
} 

/** Set a new width */ 
public void setWidth(double width) { 
    this.width = width; 
} 

/** Return height */ 
public double getHeight() { 
    return height; 
} 

/** Set a new height */ 
public void setHeight(double height) { 
    this.height = height; 
} 

/** Return area */ 
public double getArea() { 
    return width * height; 
} 

/** Return perimeter */ 
public double getPerimeter() { 
    return 2 * (width * height); 
} 

} 


public class TestCircleRectangle { 
public static void main(String[] args) { 
    CircleFromSimpleGeometricObject circle = 
      new CircleFromSimpleGeometricObject(1); 
    System.out.println("A circle " + circle.toString()); 
    System.out.println("The color is " + circle.getColor()); 
    System.out.println("The radius is " + circle.getRadius()); 
    System.out.println("The area is " + circle.getArea()); 
    System.out.println("The diamter is " + circle.getDiameter()); 

    RectangleFromSimpleGeometricObject rectangle = 
      new RectangleFromSimpleGeometricObject(2, 4); 
    System.out.println("\nA rectangle " + rectangle.toString()); 
    System.out.println("The area is " + rectangle.getArea()); 
    System.out.println("The perimeter is " + 
     rectangle.getPerimeter()); 
} 

} 
+1

나는 그 이름을 단순화 할 것이다. 좋은 이름이 중요합니다. Circle, Rectangle 및 AbstractShape가 왜 충분하지 않은지 나는 알지 못합니다. – duffymo

+0

하위 클래스는 기본적으로 수퍼 클래스 생성자를 호출합니다. super –

답변

4

public CircleFromSimpleGeometricObject(double radius) 같은 생성자는 항상 첫 번째 행으로 슈퍼 클래스의 생성자를 호출을 포함해야합니다; 명시 적으로하지 않으면 컴파일러는 수퍼 클래스의 인수가없는 생성자에 대한 호출을 보이지 않게 삽입합니다 (있는 경우). 그것은 여기서 일어난 일입니다. 생성자는 자동으로 public SimpleGeometricObject()을 호출합니다.

생성자는이 같은 슈퍼 클래스 생성자를 호출 할 수있는가 필요한 경우

super(); 

당신은 인수를 포함 할 수있다.

P. 댓글 작성자가 언급했듯이 클래스 이름은 정말 이상하고 불필요합니다. CircleRectangle이면 충분합니다.

+0

와우를 호출하여 명시 적으로 호출 할 수 있습니다. 와우, 정말 고마워요. 나는 무슨 일이 일어나고 있는지 알아 내려고 애 쓰면서 많은 시간을 보냈다. –

0

모든 생성자는 수퍼 클래스 생성자를 호출합니다 (Object 생성자가 호출 될 때까지). 명시 적으로 super()을 호출하지 않으면 컴파일러에서 자동으로 삽입합니다.

가장 확실한 방법은 수퍼 클래스에 기본 생성자가없는 것입니다 (인수가없는 생성자 없음). 이 경우 하위 클래스는 원하는 super 생성자에 대한 명시 적 호출을 삽입 할 때까지 컴파일되지 않습니다.

0

CircleFormSimpleGeometricObject이 SimpleGeometricObject를 확장하기 때문에, 생성자 SimpleGeometricObject()가 자동으로 호출됩니다 여기

내가 자바 사양에 지점이 발견 블로그입니다.

슈퍼 클래스의 생성자를 명시 적으로 호출하는 super()를 호출하는 것이 안전하지만 슈퍼 클래스에서 필요한 모든 변수가 초기화되므로 유용합니다. 사용할 생성자를 지정하기 위해 특정 생성자가 호출하는 변수를 super()에 넣을 수 있습니다. 예를 들어,이 라인은 :

super(String, boolean); 

는 SimpleGeometricObject에서 관련 생성자를 호출한다.

관련 문제