// bplc -i Account.bpl -hAccount.his local val bal = ref 0 in function deposit (amount : Nat) : () = bal := !bal + amount function withdraw (amount : Nat) : () = bal := !bal - amount function balance () : Nat = !bal end module Basic = struct // Einfache Klasse. Überführung der obigen Konstruktion // (local ref in function end) in eine Klasse. // Einzahlen, abheben, Kontostand. val account = class local val bal = ref 0 in method deposit (amount : Nat) : () = bal := !bal + amount method withdraw (amount : Nat) : () = bal := !bal - amount method balance : Nat = !bal end end val my-account = new account // Vereerbung. // Alles abheben. val extended-account = class inherit account method clear : Nat = let val money = self.balance in self.withdraw money; money end end val my-extended-account = new extended-account // Anzahl der Kontobewegungen. val supervised-account = class inherit super = extended-account in local val moves = ref 0 in method deposit (amount : Nat) : () = moves := !moves + 1; super.deposit amount method withdraw (amount : Nat) : () = moves := !moves + 1; super.withdraw amount method movements : Nat = !moves end end end val my-supervised-account = new supervised-account // Maximaler Kontrostand. val max-account = class inherit super = extended-account in local val max = ref super.balance in method deposit (amount : Nat) : () = super.deposit amount; max := Nat.maximum (!max, super.balance) method max-balance : Nat = !max end end end val my-account = new max-account end // Basic /////////////////////////////////////////////////////////////////////////////// // Konstruktoren sind klassenwertige Ausdrücke (z.B. Funktionen). module Constructor = struct // Einzahlen, abheben, Kontostand. type Account = class method deposit : Nat -> () method withdraw : Nat -> () method balance : Nat end function account (money : Nat) : Account = class local val bal = ref money in method deposit (amount : Nat) : () = bal := !bal + amount method withdraw (amount : Nat) : () = bal := !bal - amount method balance : Nat = !bal end end val my-account = new (account 4711) end // Constructor /////////////////////////////////////////////////////////////////////////////// // Klassenvariablen sind Speicherzellen, die außerhalb eines // Klassenausdrucks allokiert werden; Klassenmethoden sind Funktionen. module Class-variable = struct local val no = ref 0 in function number-of-accounts () : Nat = !no val account = class local val bal = no := !no + 1; ref 0 in method deposit (amount : Nat) : () = bal := !bal + amount method withdraw (amount : Nat) : () = bal := !bal - amount method balance : Nat = !bal end end end val my-account = new account end // Class-variable /////////////////////////////////////////////////////////////////////////////// // Eine Klasse, die Klassen generiert. Überführung der Konstruktion aus // `Class-variable' (local ref in class end) in eine Klasse. module Class-of-classes = struct type Account = class method deposit : Nat -> () method withdraw : Nat -> () method balance : Nat end type Bank = class method number-of-accounts : Nat method account : Account end val bank : Bank = class local val no = ref 0 in method number-of-accounts : Nat = !no method account : Account = class local val bal = no := !no + 1; ref 0 in method deposit (amount : Nat) : () = bal := !bal + amount method withdraw (amount : Nat) : () = bal := !bal - amount method balance : Nat = !bal end end // class account end end // class bank val my-bank = new bank // incorporate a new bank; eine neue Bank eröffnen val my-account = new (my-bank.account) // open an account; ein neues Konto eröffnen end // struct /* BPL > open Class-of-classes BPL > val my-bank = new bank val my-bank : object method account : class method deposit : Nat -> () method withdraw : Nat -> () method balance : Nat end method number-of-accounts : Nat end BPL > val my-account = new (my-bank.account) val my-account : object method balance : Nat method deposit : Nat -> () method withdraw : Nat -> () end BPL > my-account.deposit 4711 () BPL > my-account.deposit 815 () BPL > my-account.balance 5526 BPL > my-account.withdraw 1000 () BPL > my-account.balance 4526 BPL > val your-account = new (my-bank.account) val your-account : object method balance : Nat method deposit : Nat -> () method withdraw : Nat -> () end BPL > my-bank.number-of-accounts 2 */