类与对象的基本概念技朮方案.ppt

* * Object?提供的?hashCode()?的缺省实施通过将对象的内存地址对映于一个整数值来生成。 As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.) * public class AccountTester { public static void main(String args[]) { BankAccount anAccount; anAccount = new BankAccount(ZhangLi, 100023,0); anAccount.setBalance(anAccount.getBalance() + 100); System.out.println(anAccount); System.out.println(); anAccount = new BankAccount(WangFang, 100024,0); System.out.println(anAccount); anAccount.deposit(225.67f); anAccount.deposit(300.00f); System.out.println(anAccount); anAccount.withdraw(400.17f); System.out.println(anAccount); } } 应用举例 2.3.3 声明存取款方法 ——修改AccountTester.java * 测试结果 Account #100023 with balance $100.0 Account #100024 with balance $0.0 Account #100024 with balance $525.67 Account #100024 with balance $125.49997 应用举例 2.3.3 声明存取款方法 ——测试结果 * DecimalFormat类 在java.text包中 在toString()方法中使用DecimalFormat类的实例方法format对数据进行格式化 修改后的toString()方法如下 应用举例 2.3.4 使用DecimalFormat类 public String toString() { return(Account # + accountNumber + with balance + new java.text.DecimalFormat($0.00).format(balance)); } * 对BankAccount类重新进行编译,再运行BankTester类,运行结果如下 Account #100023 with balance $100.00 Account #100024 with balance $0.00 Account #100024 with balance $525.67 Account #100024 with balance $125.50 应用举例 2.3.4 使用DecimalFormat类 ——测试结果 * 声明类方法返回特殊的BankAccount实例,作用是生成特定的几种账号样例 Example1()方法 应用举例 2.3.5 声明类方法生成特殊的实例 public static BankAccount example1() { BankAccount ba = new BankAccount(); ba.setOwnerName(LiHong); ba.setAccountNumber(554000); ba.deposit(1000);

文档评论(0)

1亿VIP精品文档

相关文档