{"id":709,"date":"2022-10-06T22:47:22","date_gmt":"2022-10-06T14:47:22","guid":{"rendered":"https:\/\/qaqaq.top\/?p=709"},"modified":"2022-11-27T12:39:56","modified_gmt":"2022-11-27T04:39:56","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%ad-%e7%bb%a7%e6%89%bf%e5%92%8csuper%e8%af%be%e5%90%8e%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=709","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e2d)-\u7ee7\u627f\u548csuper\u8bfe\u540e\u7ec3\u4e60"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P279;\r\n\r\npublic class Account {\r\n\r\n\tprivate int id;\/\/ \u8d26\u53f7\r\n\tprivate double balance;\/\/ \u4f59\u989d\r\n\tprivate double annuallneterstRate;\/\/ \u5e74\u5229\u7387\r\n\r\n\tpublic Account(int id, double balance, double annuallneterstRate) {\r\n\t\tsuper();\r\n\t\tthis.id = id;\r\n\t\tthis.balance = balance;\r\n\t\tthis.annuallneterstRate = annuallneterstRate;\r\n\t}\r\n\r\n\tpublic int getId() {\r\n\t\treturn id;\r\n\t}\r\n\r\n\tpublic void setId(int id) {\r\n\t\tthis.id = id;\r\n\t}\r\n\r\n\tpublic double getBalance() {\r\n\t\treturn balance;\r\n\t}\r\n\r\n\tpublic void setBalance(double balance) {\r\n\t\tthis.balance = balance;\r\n\t}\r\n\r\n\tpublic double getAnnuallneterstRate() {\r\n\t\treturn annuallneterstRate;\r\n\t}\r\n\r\n\tpublic void setAnnuallneterstRate(double annuallneterstRate) {\r\n\t\tthis.annuallneterstRate = annuallneterstRate;\r\n\t}\r\n\r\n\t\/\/ \u8fd4\u56de\u6708\u5229\u7387\r\n\tpublic double getMonthlyInterest() {\r\n\t\treturn annuallneterstRate \/ 12;\r\n\t}\r\n\r\n\t\/\/ \u53d6\u94b1\r\n\tpublic void withdraw(double amount) {\r\n\t\tif (balance >= amount) {\r\n\t\t\tbalance -= amount;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tSystem.out.println(\"\u4f59\u989d\u4e0d\u8db3\");\r\n\t}\r\n\r\n\t\/\/ \u5b58\u94b1\r\n\tpublic void deposit(double amount) {\r\n\t\tif (amount > 0) {\r\n\t\t\tbalance += amount;\r\n\t\t}\r\n\t}\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P279;\r\n\r\n\/*\r\n * \u5199\u4e00\u4e2a\u7528\u6237\u7a0b\u5e8f\u6d4b\u8bd5 Account \u7c7b\u3002\u5728\u7528\u6237\u7a0b\u5e8f\u4e2d\uff0c\r\n * \u521b\u5efa\u4e00\u4e2a\u8d26\u53f7\u4e3a 1122\u3001\u4f59\u989d\u4e3a 20000\u3001\u5e74\u5229\u7387 4.5%\u7684 Account \u5bf9\u8c61\u3002\r\n * \u4f7f\u7528 withdraw \u65b9\u6cd5\u63d0\u6b3e 30000 \u5143\uff0c\u5e76\u6253\u5370\u4f59\u989d\u3002\r\n * \u518d\u4f7f\u7528 withdraw \u65b9\u6cd5\u63d0\u6b3e 2500 \u5143\uff0c\r\n * \u4f7f\u7528 deposit \u65b9\u6cd5\u5b58\u6b3e 3000 \u5143\uff0c\u7136\u540e\u6253\u5370\u4f59\u989d\u548c\u6708\u5229\u7387\u3002\r\n * \r\n * \r\n * \r\n * \r\n * \r\n *\/\r\npublic class AccountTest {\r\n\tpublic static void main(String&#91;] args) {\r\n\r\n\t\tAccount acct = new Account(1122, 20000, 0.045);\r\n\r\n\t\tacct.withdraw(30000);\r\n\t\tSystem.out.println(\"\u60a8\u7684\u8d26\u6237\u4f59\u989d\u4e3a\uff1a\" + acct.getBalance());\r\n\t\tacct.withdraw(2500);\r\n\t\tSystem.out.println(\"\u60a8\u7684\u8d26\u6237\u4f59\u989d\u4e3a\uff1a\" + acct.getBalance());\r\n\t\tacct.deposit(3000);\r\n\t\tSystem.out.println(\"\u60a8\u7684\u8d26\u6237\u4f59\u989d\u4e3a\uff1a\" + acct.getBalance());\r\n\r\n\t\tSystem.out.println(\"\u6708\u5229\u7387\u4e3a\uff1a\" + (acct.getMonthlyInterest()) * 100 + \"%\");\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\r\n * \u521b\u5efa Account \u7c7b\u7684\u4e00\u4e2a\u5b50\u7c7b CheckAccount \u4ee3\u8868\u53ef\u900f\u652f\u7684\u8d26\u6237\uff0c\u8be5\u8d26\u6237\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5c5e\u6027 overdraft \u4ee3\u8868\u53ef\u900f\u652f\u9650\u989d\u3002\r\n * \u5728 CheckAccount \u7c7b\u4e2d\u91cd\u5199 withdraw \u65b9\u6cd5\uff0c\u5176\u7b97\u6cd5\u5982\u4e0b\uff1a\r\n\t\u5982\u679c\uff08\u53d6\u6b3e\u91d1\u989d&lt;\u8d26\u6237\u4f59\u989d\uff09\uff0c\r\n\t\u53ef\u76f4\u63a5\u53d6\u6b3e\r\n\t\u5982\u679c\uff08\u53d6\u6b3e\u91d1\u989d>\u8d26\u6237\u4f59\u989d\uff09\uff0c\r\n\t\u8ba1\u7b97\u9700\u8981\u900f\u652f\u7684\u989d\u5ea6\r\n\t\u5224\u65ad\u53ef\u900f\u652f\u989d overdraft \u662f\u5426\u8db3\u591f\u652f\u4ed8\u672c\u6b21\u900f\u652f\u9700\u8981\uff0c\u5982\u679c\u53ef\u4ee5\r\n\t\t\u5c06\u8d26\u6237\u4f59\u989d\u4fee\u6539\u4e3a 0\uff0c\u51b2\u51cf\u53ef\u900f\u652f\u91d1\u989d\r\n\t\u5982\u679c\u4e0d\u53ef\u4ee5\r\n\t\t\u63d0\u793a\u7528\u6237\u8d85\u8fc7\u53ef\u900f\u652f\u989d\u7684\u9650\u989d\r\n * \r\n * \r\n * \r\n * \r\n * \r\n *\/\r\npackage top.qaqaq.java.P279;\r\n\r\npublic class CheckAccount extends Account {\r\n\r\n\tprivate double overdraft;\/\/ \u53ef\u900f\u652f\u9650\u989d\r\n\r\n\tpublic CheckAccount(int id, double balance, double annuallneterstRate, double overdraft) {\r\n\t\tsuper(id, balance, annuallneterstRate);\r\n\t\tthis.overdraft = overdraft;\r\n\t}\r\n\r\n\tpublic double getOverdraft() {\r\n\t\treturn overdraft;\r\n\t}\r\n\r\n\tpublic void setOverdraft(double overdraft) {\r\n\t\tthis.overdraft = overdraft;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void withdraw(double amount) {\r\n\t\tif (getBalance() >= amount) {\/\/ \u4f59\u989d\u5c31\u8db3\u591f\u6d88\u8d39\r\n\/\/\t\t\tgetBalance() -= amount;\r\n\t\t\t\/\/ \u65b9\u5f0f\u4e00\uff1a\r\n\/\/\t\t\tsetBalance(getBalance() - amount);\r\n\t\t\t\/\/ \u65b9\u5f0f\u4e8c\uff1a\r\n\t\t\tsuper.withdraw(amount);\r\n\t\t} else if (overdraft >= amount - getBalance()) {\/\/ \u900f\u652f\u989d\u5ea6+\u4f59\u989d\u8db3\u591f\u6d88\u8d39\r\n\r\n\t\t\toverdraft -= (amount - getBalance());\r\n\r\n\/\/\t\t\tsetBalance(0);\r\n\t\t\t\/\/ \u6216\r\n\t\t\tsuper.withdraw(getBalance());\r\n\r\n\t\t} else {\r\n\t\t\tSystem.out.println(\"\u8d85\u8fc7\u53ef\u900f\u652f\u9650\u989d\uff01\");\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P279;\r\n\r\n\/*\r\n * \u5199\u4e00\u4e2a\u7528\u6237\u7a0b\u5e8f\u6d4b\u8bd5 CheckAccount \u7c7b\u3002\r\n * \u5728\u7528\u6237\u7a0b\u5e8f\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u8d26\u53f7\u4e3a 1122\u3001\u4f59\u989d\u4e3a 20000\u3001\u5e74\u5229\u7387 4.5%\uff0c\u53ef\u900f\u652f\u9650\u989d\u4e3a 5000 \u5143\u7684 CheckAccount \u5bf9\u8c61\u3002\r\n\t\u4f7f\u7528 withdraw \u65b9\u6cd5\u63d0\u6b3e 5000 \u5143\uff0c\u5e76\u6253\u5370\u8d26\u6237\u4f59\u989d\u548c\u53ef\u900f\u652f\u989d\u3002\r\n\t\u518d\u4f7f\u7528 withdraw \u65b9\u6cd5\u63d0\u6b3e 18000 \u5143\uff0c\u5e76\u6253\u5370\u8d26\u6237\u4f59\u989d\u548c\u53ef\u900f\u652f\u989d\u3002\r\n\t\u518d\u4f7f\u7528 withdraw \u65b9\u6cd5\u63d0\u6b3e 3000 \u5143\uff0c\u5e76\u6253\u5370\u8d26\u6237\u4f59\u989d\u548c\u53ef\u900f\u652f\u989d\u3002\r\n * \r\n * \r\n * \r\n * \r\n * \r\n * \r\n *\/\r\npublic class CheckAccountTest {\r\n\r\n\tpublic static void main(String&#91;] args) {\r\n\r\n\t\tCheckAccount acct = new CheckAccount(1122, 20000, 0.045, 5000);\r\n\r\n\t\tacct.withdraw(5000);\r\n\t\tSystem.out.println(\"\u60a8\u7684\u8d26\u6237\u4f59\u989d\u4e3a\uff1a\" + acct.getBalance());\r\n\t\tSystem.out.println(\"\u60a8\u7684\u53ef\u900f\u652f\u989d\u5ea6\u4e3a\uff1a\" + acct.getOverdraft());\r\n\t\tacct.withdraw(18000);\r\n\t\tSystem.out.println(\"\u60a8\u7684\u8d26\u6237\u4f59\u989d\u4e3a\uff1a\" + acct.getBalance());\r\n\t\tSystem.out.println(\"\u60a8\u7684\u53ef\u900f\u652f\u989d\u5ea6\u4e3a\uff1a\" + acct.getOverdraft());\r\n\t\tacct.withdraw(3000);\r\n\t\tSystem.out.println(\"\u60a8\u7684\u8d26\u6237\u4f59\u989d\u4e3a\uff1a\" + acct.getBalance());\r\n\t\tSystem.out.println(\"\u60a8\u7684\u53ef\u900f\u652f\u989d\u5ea6\u4e3a\uff1a\" + acct.getOverdraft());\r\n\t}\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-709","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/709"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=709"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/709\/revisions"}],"predecessor-version":[{"id":710,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/709\/revisions\/710"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}