2013-07-09 212 views
2

我有一個擴展到兩個子類(CircleFromSimpleGeometricObject和RectangleFromSimpleGeometricObject)的超類(SimpleGeometricObject),以及一個調用CircleFromSimpleGeometricObject和RectangleFromSimpleGeometricObject的類,名爲TestCircleRectangle。繼調試,爲子類CircleFromSumpleGeometricObject, 這行代碼:Java超類繼承

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

莫名其妙地調用超SimpleGeometricObject:

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

我有點困惑如何發生以及爲什麼有人可以幫助我理解爲什麼發生這種情況以下是所有課程的代碼。

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.S.作爲一位評論者提到,你的班級名稱非常奇怪而且沒有必要; CircleRectangle就足夠了。

+0

來明確地調用它,非常感謝。我花了很多時間試圖弄清楚發生了什麼。 –

0

任何構造函數調用超類構造函數(依此類推,直到調用Object構造函數爲止)。如果您沒有明確地調用super(),則編譯器將爲您插入它。

看到這個最明顯的方法是在你的超類中沒有默認構造函數(沒有沒有任何參數的構造函數)。在這種情況下,除非您插入對所需super構造函數的明確調用,否則子類將不會編譯。

0

由於CircleFormSimpleGeometricObject延伸SimpleGeometricObject,構造SimpleGeometricObject()自動構造CircleFormSimpleGeometricObject()被調用時調用。

這很有用,因爲子類需要從超類中得到的任何變量都會被初始化,但如果是這樣的話,調用超類的構造函數super()會更安全。要指定要使用哪個構造函數,可以將特定構造函數要求的變量放入super()中。例如,以下行:

super(String, boolean); 

將調用SimpleGeometricObject中的相關構造函數。