【问题标题】:Constructor of subclass子类的构造函数
【发布时间】: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


【解决方案1】:

你的子类CategoryAssignment不应该调用超类的构造函数吗?比如:

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

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

关于您的第二个问题“这也是一个成绩册项目,将类别变量存储在数组或 ArrayList 中是否明智?”,据我在 getter 中所见,cat 变量是一个字符串。很难判断列表或数组是否最适合您提供的信息。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 另外,成员cat没有定义(第二类中的注释部分详细说明)
    【解决方案3】:

    首先: 如果你想访问它,你必须在你的类中声明变量 cat。你必须在你的课堂上添加这个:

        private String cat;
    

    下一步: 子类的构造函数中的第一条语句是超类的构造函数。在你的情况下:

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

    【讨论】:

      【解决方案4】:

      我认为您需要考虑从子类调用基类的构造函数。

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

      【讨论】:

      • 这个问题是在 JAVA 中,而不是 C#
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多