【发布时间】:2016-12-02 16:22:52
【问题描述】:
这是主类
**
** 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 中是否明智?
【问题讨论】:
-
什么错误?请显示堆栈跟踪。另外,请正确格式化代码
-
我从未在类中看到
cat定义。
标签: java inheritance constructor subclass superclass