第2章 类与对象的基本概念详解.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* * 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精品文档

相关文档