2016-12-02 43 views
-1

的這裏的主類構造子

** 
** Assignment class 
** 
** This class represents an Assignment. 
** 
****************************************************/ 
public class Assignment { 
private String name; 
private double pointsPossible; 
private double pointsEarned; 

// Assignment constructor 
// 
// postcondition: all instance variables are initialized with 
// the given values. 
public Assignment (String n, double ptsPoss, double ptsEarned) { 
name =n; 
pointsPossible=ptsPoss; 
pointsEarned=ptsEarned; 
} 
// getName accessor method 
// 
// postcondition: returns the name of this Assignment public 
String getName() { 
return name; 
    } 

// getPointsPossible accessor method 
// 
// postcondition: returns the points possible for this Assignment 
public double getPointsPossible() { 
return pointsPossible; 
    } 
// getPointsEarned accessor method 
// 
// postcondition: returns the points earned for this Assignment 
public double getPointsEarned() { 
return pointsEarned; 
} 
} 

,當我嘗試使用我的訪問者在我的子類,我得到一個錯誤試圖初始化它的變量

這裏的子類

import java.util.ArrayList; 
/**************************************************** 
** 
** CategoryAssignment class 
** 
** This class represents an CategoryAssignment. 
** Do not add any additional methods to this class. 
** 
****************************************************/ 
public class CategoryAssignment extends Assignment { 
    // declare any new instance variables that you need here 
    // don't forget to make them private! 
    // don't declare more that you really need! 
    // CategoryAssignment constructor 
    // 
    // postcondition: all instance variables are initialized with 
    // the given values.  
    public CategoryAssignment (String n, double ptsPoss, double ptsEarned, String cat) { 

    } 

    // getCategoryName accessor method 
    // 
    // postcondition: returns the name of the category associated 
    // with this CategoryAssignment 
    public String getCategoryName() { 
     return cat; 
    } 
} 

我無法獲得子類的變量初始化。還有,這是一個成績冊項目,將類別變量存儲在數組或ArrayList中會很明智嗎?

+3

什麼錯誤?請顯示堆棧跟蹤。此外,請正確格式化代碼 – ItamarG3

+0

我從不在類中定義'cat'。 – Compass

回答

1

您的子類CategoryAssignment不應該調用超類構造函數嗎?喜歡的東西:

public CategoryAssignment (String n, double ptsPoss, double ptsEarned, String cat) { 
    super(n, ptsPoss, ptsEarned); 
    this.cat = cat; 
} 

您還需要在CategoryAssignment定義String cat屬性。

關於你提到的第二個問題「也與此是一個檔次的書的項目,將是明智的存儲數組或一個ArrayList變量的類別?」,據我可以在吸氣看到,cat變量是一個字符串。很難判斷一個列表或一個數組是否最適合您提供的信息。

0

即使子類'和超類'構造函數具有相同的簽名,在Java中也沒有隱式構造函數鏈。你需要的參數明確地傳遞給父類的構造函數與super關鍵字:

public CategoryAssignment 
    (String n, double ptsPoss, double ptsEarned, String cat) { 
    super(n, ptsPoss, ptsEarned, car); 
} 
+0

此外,成員'貓'沒有定義(第二課中的評論部分詳細說明了這個) – ItamarG3

0

我認爲你需要看看從你的子類調用你的基類構造函數。

public CategoryAssignment (String n, double ptsPoss, double ptsEarned, String cat) { 
    base(n, ptsPoss,ptsEarned, cat); 
} 
+0

這個問題是在JAVA中,而不是C# – ItamarG3

0

首先: 如果你想訪問這個,你必須在你的類中聲明變量cat。你必須在你的類補充一點:

private String cat; 

下一頁: 子類中的第一條語句的構造函數是父類的構造函數。在你的情況下:

public CategoryAssignment(String n, double ptsPoss, double ptsEarned, String cat) { 
     super(n, ptsPoss, ptsEarned); 
     this.cat = cat; 
    }