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

时间:2013-05-27 00:41:39

标签: java variables abstract-class

我有一个抽象类“HotelReviewClass”& “RestaurantReviewClass”,我想初始化不是commun和commun的变量去“超级”类

但我有一个错误“构造函数调用必须是构造函数中的第一个语句” 我如何初始化那些不公开的变量(因为“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);
    }

}

1 个答案:

答案 0 :(得分:3)

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

只需将通话转到第一行:

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