java - 在抽象构造函数类中初始化变量

标签 java variables abstract-class

我有一个抽象类“HotelReviewClass”和“RestaurantReviewClass”,我想在其中初始化非公共(public)变量,公共(public)变量转到“ super ”类

但是我有一个错误“构造函数调用必须是构造函数中的第一条语句” 我如何初始化不常见的变量(因为“HotelReviewClass”和“RestaurantReviewClass”变量不相等)

package pt;

public class HotelReviewClass extends AbstractReview{

    private String ratingService;
    private String ratingLocal;

    public HotelReviewClass(String grade, String comment, String service, String local, String owner){
        this.ratingService = service;
        this.ratingLocal = local;
        super(grade, comment, owner);
    }

}

最佳答案

任何调用超构造函数的构造函数的第一行必须是对超构造函数的调用。

只需将调用移至第一行:

public HotelReviewClass(String grade, String comment, String service, String local, String owner){
    super(grade, comment, owner);
    this.ratingService = service;
    this.ratingLocal = local;
}

关于java - 在抽象构造函数类中初始化变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16765029/

相关文章:

java - SCTP INIT 缺少 IPv4 地址参数

bash - 是否可以将变量从 BASH 脚本传递到 Linux 上的 PWSH

javascript - 查找 div ID 并使用 jQuery 调用关联的 Javascript 变量

c# - Visual Studio 设计器中的抽象 UserControl 继承

python - Python 文档中 "mixin methods"的含义

actionscript-3 - 您如何决定使用抽象类和接口(interface)?

java - 打包maven项目时出错: launch4j. BuilderException

java - 奇怪的 JMenu 位置问题

java - 如何查看keytool导入的keystore中导入的证书?

javascript - 使用 Javascript 将文本和变量写入 HTML