Bank Account public class Bank Account public double
Bank. Account שאילתות public class Bank. Account { public double get. Balance() {. . . ; } public long get. Account. Number() {. . . ; } שאילתות public Customer get. Owner () {. . . ; } private double balance; private long account. Number; private Customer owner; מצב פנימי תעשה field הגישה לשדה : • מוסכמה . get. Field() בעזרת המתודה • שמירה על מוסכמה זו הכרחית בסביבות Java. Beans - ו GUI Builders } 9
משיכה והפקדה : פקודות public void deposit(double amount) { balance += amount; } this. balance - שקול ל חוזה : ' אפשרות א /** * Withdraw amount from the account * * @pre 0? ? ? ? ? ? ? ? ? ? < amount <= get. Balance() * @post get. Balance() == $prev(get. Balance()) - amount ? ? ? ? ? ? ? ? ? ? */ public void withdraw(double amount) { balance -= amount; } 11
משיכה והפקדה : פקודות public void deposit(double amount) { balance += amount; } /** * Withdraw amount from the account * * @pre 0 < amount <= get. Balance() * @post get. Balance() == $prev(get. Balance()) - amount */ public void withdraw(double amount) { if (amount < 0 || amount > get. Balance()) { balance -= amount; System. out. println("Invalid withdrawal amount: "+ } amount); return; } balance -= amount; 12 : תכנות מתגונן : ' אפשרות ב }
דיון – העברה בנקאית : מספר חלופות למימוש העברת סכום מחשבון לחשבון מתודה סטטית שתקבל שני חשבונות : אפשרות א n : בנק ותבצע ביניהם העברה /** * Makes a transfer of amount from one account to the other * @pre 0 < amount <= from. get. Balance() * @post to. get. Balance() == $prev(to. get. Balance()) + amount * @post from. get. Balance() == $prev(from. get. Balance()) – amount */ public static void transfer(double amount, Bank. Account from, Bank. Account to) { from. withdraw(amount); to. deposit(amount); } 13
דיון – העברה בנקאית , אחד החשבונות אחראי לפעולה )למשל : אפשרות ב n ( מעביר הכסף /** * Makes a transfer of amount from the current * account to the other one */ public void transfer. To(double amount, withdraw Bank. Account other) { other. deposit(amount); withdraw(amount); } אפשר גם להשתמש בהעמסה withdraw של 14
Bank. Account בנאי /** * Constructs a new account and sets its owner and identifier * @pre id > 0 * @pre customer != null * @post get. Owner() == customer * @post get. Account. Number() == id * @post get. Balance() == 0 */ public Bank. Account(Customer customer, long id) { account. Number = id; owner = customer; } ! אין ערך החזרה לבנאי מכאן new Bank. Account - לא נקרא ל null אם יש בעיה בקלט אי אפשר להחזיר 16
Current. Class המחלקה public class Current. Class { public static void my. Public. Static. Method() { System. out. println("In my. Public. Static. Method"); } private static void my. Private. Static. Method() { System. out. println("In my. Private. Static. Method"); } public void my. Public. Method() { System. out. print("In my. Public. Method >> "); my. Private. Method(); פומבית } קריאה למתודה פרטית ממתודה ( )גם ההפך זה בסדר private void my. Private. Method() { System. out. println("In my. Private. Method"); } } 19
Other. Class המחלקה public class Other. Class { public static void others. Public. Static. Method() { System. out. println("In others. Public. Static. Method"); } private static void others. Private. Static. Method() { System. out. println("In others. Private. Static. Method"); } public void others. Public. Method() { System. out. print("In others. Public. Method >> "); others. Private. Method(); } private void others. Private. Method() { System. out. println("In others. Private. Method"); } } 20
Current. Class- ל main נוסיף public class Current. Class { public static void main(String[] args) { Current. Class. my. Public. Static. Method(); // Prints: In my. Public. Static. Method(); Current. Class. my. Private. Static. Method(); // Prints: In my. Private. Static. Method Current. Class. my. Public. Method(); Current. Class current. Class = new Current. Class(); current. Class. my. Public. Method(); // Prints: In my. Public. Method >> In my. Private. Method current. Class. my. Private. Method(); // Prints: In my. Private. Method current. Class. my. Public. Static. Method(); //Has a warning, Prints: In my. Public. Static. Method Other. Class. others. Public. Static. Method(); // Prints: In others. Public. Static. Method(); Other. Class. others. Private. Static. Method(); Other. Class other. Class = new Other. Class(); other. Class. others. Public. Method(); // Prints: In others. Public. Method >> In others. Private. Method other. Class. others. Private. Method(); }. . . 21
דוגמא public class Bank. Account { public static final String BANK_NAME = "BNP"; //static constant private static int last. Account. Id = 0; //static field private int id; public Bank. Account() { id = ++last. Account. Id; // unique ID for every account } /* static method */ public static void main(String[] args) { System. out. println(last. Account. Id); System. out. println(id); Bank. Account account = new Bank. Account(); System. out. println(account. id); } /* instance method */ public void print. Stuff() { System. out. println(last. Account. Id); System. out. println(id); } } 24 Why? ?
- Slides: 25