我有一个抽象类“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/