2012-08-10 57 views
0

我正在爲我的課程進行練習,並且通過編碼偶然發現了一個問題。我應該做一個擴展類,我認爲我遇到的問題是我給構造函數的參數。找不到符號Java

這裏是直接超類:

public class ElectricalComponent extends Component 
{ 
    private int myMinRating, 
       myMaxRating; 

    public ElectricalComponent(String partNumber, int versionNumber, int minRating, int maxRating) 
    { 
    super("Electrical", partNumber, versionNumber); 
    myMinRating = minRating; 
    myMaxRating = maxRating; 
    } 

    public int getMinRating() { return myMinRating; } 
    public int getMaxRating() { return myMaxRating; } 
} 

這裏是我工作類:

public class HighvoltageComponent extends ElectricalComponent 
{ 
    private int myMinRating, myMaxRating; 

    public HighvoltageComponent(String partNumber, int versionNumber) 
    { 
    super("Electrical", partNumber, versionNumber); 
    myMinRating = 50000; 
    myMaxRating = 200000; 
    } 

} 

我的問題是在那裏說子類:「HighvoltageComponent(字符串。 ..)「

當運行主類,這是

public static void main(String[] args) 
{ 
    // test your code here 
    Component a = new HighvoltageComponent("HV12", 0); 

    System.out.println(a.toString()); 
    System.out.println(a.getTypeName()); 
    System.out.println(a.getPartNumber()); 
    System.out.println(a.getVersionNumber()); 
} 

我得到那個說

錯誤 「HighvoltageComponent.java:9:找不到符號

符號:構造ElectricalComponent(java.lang.String中,java.lang.String中,INT)」

這是怎麼發生的?

另外,你能告訴我,如果我正確地做這個問題嗎?這是一個問題:

HighvoltageComponent是一個ElectricalComponent,最小額定值爲50000,最大額定值爲200000.完成HighvoltageComponent的以下定義。 (您需要在下面的代碼區多個地方插入代碼。)

感謝,羅漢

回答

2

ElectricalComponent構造函數需要四個參數,你只能在電話super("Electrical", partNumber, versionNumber); 3個PARAMS在HighvoltageComponent

通過

你超級調用應該是這樣的

super(partNumber, versionNumber,myMinRating ,myMaxRating); 
+0

所以它應該是公共的HighvoltageComponent(String partNumber,int versionNumber,int minRange,int maxRange)? – Panthy 2012-08-10 03:04:50

+0

查看更新的答案。 – kosa 2012-08-10 03:09:13

+0

但我需要「電氣」在那裏?我認爲? – Panthy 2012-08-10 03:13:03

0

由於所有ElectricalComponent擴展s被發現有minRatingmaxRating,您可以更好地更改這兩個字段的範圍類,並在其構造函數中刪除這兩個參數。這可以避免在父實例和子實例中保持相同的值。

-1
public class HighvoltageComponent extends ElectricalComponent 
{ 

    public HighvoltageComponent( String partNumber, int versionNumber) 
    { 

    super(partNumber, versionNumber, 50000 ,200000); 

    } 


} 
+0

給這個答案增加一些解釋可能會使這個答案更好。 – NathanOliver 2015-08-10 17:37:26