我无法访问 getter

时间:2021-03-27 14:41:29

标签: getter setter

我在 BankAccountServeice 类中创建了 getter。 像这样:

public class BankAccountServeice {
private List<BankAccountServeice> List;


private int accountnumber;
private int pin;
private int balance;

public BankAccountServeice(int accountnumber, int pin, int balance) {

    this.accountnumber = accountnumber;
    this.pin = pin;
    this.balance = balance;

}

public void initalizeBankAccounts() {       
    List = new ArrayList<>();
    List.add(new BankAccountServeice(111, 1234, 15000));
    List.add(new BankAccountServeice(222, 2345, 10000));
    List.add(new BankAccountServeice(333, 3456, 400000));
}

public int getAccountnumber() {
    return accountnumber;
}

public int getPin() {
    return pin;
}

public int getBalance() {
    return balance;
}

public void setBalance(int balance) {
    this.balance = balance;
}

当我将类更改为不同的类时,我无法使用我的 getter。

public class CarService {
private List<Car> cars;
private List<Person> persons;
private Car car;
private List<BankAccountServeice> List;


public CarService(Scanner scanner) {
   
    initalizeCars();
    initalizePeople();
    System.out.println(getPin());
    System.out.println(getBalance());
    System.out.println(getAccountnumber());}

}

我应该怎么做才能访问这些 getter?我应该提供 BankAccountService 类的路径吗?

1 个答案:

答案 0 :(得分:0)

getter 是 BankAccountServeice 类的方法(我猜这应该是 BankAccountService)。因此要使用它们,首先需要创建该类的对象。 例如:

BankAccountServeice A = new BankAccountServeice(5, 12, 60);

然后您可以像这样调用该对象的 getter:

A.getPin();
A.getBalance();
A.getAccountnumber();
相关问题