小编San*_*ith的帖子

当一个类从一个抽象类扩展然后如何访问它的私有变量?

我有一个抽象类A,类B从它扩展.我将这些变量设为私有且很好.

public abstract class A  {
    private String name;
    private String location;

public A(String name,String location) {
        this.name = name;
        this.location = location;
}
 public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    public String getLocation() {
        return location;
    }
Run Code Online (Sandbox Code Playgroud)

然后我想写B班.

public class B extends A{
private int fee;
private int goals;   // something unique to class B
Run Code Online (Sandbox Code Playgroud)

我不明白如何为类B编写构造函数来访问它的私有变量.我写了这样的东西,错了.

    B(int fee, int goals){
       this.fee= fee;
       this.goals=goals;
     }
Run Code Online (Sandbox Code Playgroud)

你可以帮我解释一下这个简短的解释.

java constructor abstract-class

2
推荐指数
1
解决办法
90
查看次数

标签 统计

abstract-class ×1

constructor ×1

java ×1