class BankAccount { /* * Fields, atributes */ long accountNumber; double balance; String customerName; /* * Constructor */ BankAccount(long number, double openingBal, String customer) { // Code to create a new BankAccount object accountNumber = number; balance = openingBal; customerName = customer; } /* * Methods */ void deposit(double amount) { // code to deposit amount balance += amount; } void withdraw(double amount) { // code to withdraw amount balance -= amount; } // any additional methods, constructors and/or fields }
Použití objektů třídy BankAccount. Nejdříve vytvoření objektů.
BankAccount account1 = new BankAccount(123456, 1E6, "J DOe"); BankAccount account2 = new BankAccount(123457, 112E6, "D Beckham"); double difference = account2.balance - account1.balance; System.out.println(account2.customerName + " has " + difference + " more than " + account1.customerName); account2.withdraw(difference); account1.deposit(difference); account1 = account2;