子类的构造器

Constructor of subclass

这是主要内容class

    **
** 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;
 }
}

当我尝试在子class中使用访问器时,我在尝试初始化其变量时遇到错误

这是子class

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;
    }
}

我无法获取 subclass 的变量进行初始化。此外,由于这是一个成绩册项目,将类别变量存储在数组或 ArrayList 中是否明智?

Java 中没有隐式构造函数链接这样的东西,即使子类和超类的构造函数具有相同的签名。您需要使用 super 关键字将参数显式传递给超类的构造函数:

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

我认为您需要考虑从子 class.

调用基于 class 的构造函数
public CategoryAssignment (String n, double ptsPoss, double ptsEarned, String cat) {
    base(n, ptsPoss,ptsEarned, cat);
}

你的 subclass CategoryAssignment 不应该调用 super class 构造函数吗?类似于:

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

您还需要在 CategoryAssignment 中定义 String cat 属性。

关于你的第二个问题"Also with this being a grade book project would it be smart to store the categories variable in an array or an ArrayList?",据我在getter中看到的,cat变量是一个字符串。很难判断列表或数组是否最适合您提供的信息。

首先: 如果您想访问它,您必须在 class 中声明变量 cat。您必须在 class 中添加:

    private String cat;

下一个: subclass' 构造函数中的第一条语句是 superclass 的构造函数。你的情况:

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