Java 子类构造函数
这是主要课程Java 子类构造函数,java,inheritance,constructor,subclass,superclass,Java,Inheritance,Constructor,Subclass,Superclass,这是主要课程 ** ** Assignment class ** ** This class represents an Assignment. ** ****************************************************/ public class Assignment { private String name; private double pointsPossible; private double pointsEarned; // Ass
**
** 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;
}
}
我无法初始化子类的变量。此外,作为一个成绩册项目,将categories变量存储在数组或ArrayList中是否明智?Java中没有隐式构造函数链接,即使子类和超类构造函数具有相同的签名。您需要使用
super
关键字显式地将参数传递给超类的构造函数:
public CategoryAssignment
(String n, double ptsPoss, double ptsEarned, String cat) {
super(n, ptsPoss, ptsEarned, car);
}
我认为您需要考虑从子类调用基类上的构造函数
public CategoryAssignment (String n, double ptsPoss, double ptsEarned, String cat) {
base(n, ptsPoss,ptsEarned, cat);
}
您的子类
categoryaSignment
不应该调用超级类构造函数吗?比如:
public CategoryAssignment (String n, double ptsPoss, double ptsEarned, String cat) {
super(n, ptsPoss, ptsEarned);
this.cat = cat;
}
您还需要在类别签名
中定义字符串cat
属性
关于你的第二个问题“这也是一个成绩册项目,将categories变量存储在数组或ArrayList中是否明智?”,据我在getter中看到的,cat
变量是一个字符串。很难说列表或数组是否最适合您提供的信息。首先:
如果你想访问这个函数,你必须在你的类中声明变量cat。您必须在类中添加以下内容:
private String cat;
下一步:
子类构造函数中的第一条语句是超类的构造函数。就你而言:
public CategoryAssignment(String n, double ptsPoss, double ptsEarned, String cat) {
super(n, ptsPoss, ptsEarned);
this.cat = cat;
}
什么错误?请出示stacktrace。另外,请正确设置代码的格式。我从未看到类中定义的
cat
。此外,未定义成员cat
(第二个类中的注释部分详细说明了这一点)。此问题是用JAVA编写的,而不是用C编写的#