我试图在多个类中使用setter / getter类来修改单个变量。我知道必须使用相同的对象才能使它起作用,但是我不确定如何使多个类可以访问单个对象。

public class accountbalance {
    private double balance;

    //getter
    public double getBalance() {
        return balance;
    }

    //Setter
    public void setBalance(double newBalance) {
        this.balance = newBalance;
    }

}


这是我首先尝试使用的地方:

public class CreateAccount {

    String name;
    double social;
    int pin;

    public CreateAccount()
    {
        name = "null";
        social = 0;
        pin = 0;
    }

    public CreateAccount(String enteredName, double enteredSocial,int enteredPin, double value)
    {
        name = enteredName;
        social = enteredSocial;
        pin = enteredPin;
        double accountnum = 87495;

        accountbalance a1 = new accountbalance();
        a1.setBalance(value);

        System.out.println(name);
        System.out.println(social);
        System.out.println("Your new PIN is " + pin);
        System.out.println("Your new account balance is " + (a1.getBalance()));

    }
}



然后我在这里尝试再次使用它:

public class deposit {

    double enteredAmt;
    double amt;

    public void deposit() {
        System.out.println("Enter an amount to desposit: ");
        Scanner in = new Scanner(System.in);
        enteredAmt = in.nextDouble();

        accountbalance ab1 = new accountbalance();

        System.out.println("current balence: " + ab1.getBalance());

        amt = ab1.getBalance() + enteredAmt;
        ab1.setBalance(amt);
        System.out.println("Your new balance is " + (ab1.getBalance()));
    }
}

最佳答案

我相信您要尝试使用的是Command Design Pattern

如果您将类更改为更像这样,则效果可能会更好一些。

public class Account {

    // Please do not use SS for an identifier.
    private String identifier;

    private String name;

    // Money needs to be stored in it's lowest common denominator.
    // Which in the united states, is pennies.
    private long balance;

    public Account(String identifier, String name) {
        this.identifier = identifier;
        this.name = name;
        this.balance = 0L;
    }

    public String getIdentifier() {
        return this.identifier;
    }

    public String getName() {
        return this.name;
    }

    public long getBalance() {
        return balance;
    }

    public void credit(long amount) {

        balance =+ amount;
    }

    public void debit(long amount) {
        balance =- amount;
    }
}


然后这将是您的CreateAccountCommand。有点贫血,但是没关系。

public class CreateAccountCommand {

    private String identifier;
    private String name;

    public CreateAccount(String identifier, String name) {

        // Identifier sould actually be generated by something else.
        this.identifier = identifier;
        name = name;
    }

    public Account execute() {

        return new Account(identifier, name);
    }
}


这是您的DepositCommand:

public class DepositCommand {

    private Account account;
    private long amount;

    public DepositCommand(Account account, long amount) {
        this.account = account;
        this.amount = amount;
    }

    public void execute() {

        this.account.credit(amount);
    }
}


WithdrawCommand:

public class WithdrawCommand {

    private Account account;
    private long amount;

    public DepositCommand(Account account, long amount) {
        this.account = account;
        this.amount = amount;
    }

    public void execute() {

        this.account.debit(amount);
    }
}


然后运行所有内容:

public class Run {
    public static void main(String... args) {

        CreateAccountCommand createAccountCommand = new CreateAccountCommand("12345678", "First_Name Last_Name");

        Account account = createAccountCommand.execute();

        System.out.println(account.getIdentifier());
        System.out.println(account.getName());
        System.out.println("Your new account balance in pennies: " + account.getBalance());

        // Deposit $100.00
        DepositCommand depositCommand = new DepositCommand(account, 10000);

        depositCommand.execute();

        System.out.println(account.getIdentifier());
        System.out.println(account.getName());
        System.out.println("Your new account balance in pennies: " + account.getBalance());

        // Withdraw $75.00
        WithdrawCommand withdrawCommand = new WithdrawCommand(account, 7500);
        withdrawCommand.execute();

        System.out.println(account.getIdentifier());
        System.out.println(account.getName());
        System.out.println("Your new account balance in pennies: " + account.getBalance());
    }
}

09-30 14:56
查看更多