Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 子类构造函数_Java_Inheritance_Constructor_Subclass_Superclass - Fatal编程技术网

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编写的#