static应用举例
2021/9/11 6:05:00
本文主要是介绍static应用举例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1 package com.fu.statictest; 2 3 /** 4 * static应用举例: 5 * 编写一个类实现银行账户的概念,包含的属性有 “账号”、“密码”、“存款余额”、“利率”、“最小余额”, 6 * 定义封装这些属性的方法,账户自动生成。 7 * 编写主类,使用银行账户类,输入,输出3个储蓄的上述信息。 8 * 考虑:哪些属性可以设计为static属性 9 * 10 * 11 * 12 */ 13 public class Account { 14 private int id; 15 private String password = "000000"; 16 private double balance = 0.0; 17 private static double interestRate; 18 private static double minBalance = 1.0; 19 private static int init = 1001;//用于自动生成账户使用的 20 21 public Account(){ 22 id = init++; 23 } 24 25 public Account(String password, double balance) { 26 this(); 27 this.password = password; 28 this.balance = balance; 29 } 30 31 public int getId(){ 32 return id; 33 } 34 35 public String getPassword() { 36 return password; 37 } 38 39 public void setPassword(String password) { 40 this.password = password; 41 } 42 43 public double getBalance() { 44 return balance; 45 } 46 47 public static double getInterestRate() { 48 return interestRate; 49 } 50 51 public static void setInterestRate(double interestRate) { 52 Account.interestRate = interestRate; 53 } 54 55 public static double getMinBalance() { 56 return minBalance; 57 } 58 59 public static void setMinBalance(double minBalance) { 60 Account.minBalance = minBalance; 61 } 62 63 @Override 64 public String toString() { 65 return "Account{" + 66 "id=" + id + 67 ", password='" + password + '\'' + 68 ", balance=" + balance + 69 '}'; 70 } 71 }
package com.fu.statictest; public class AccountTest { public static void main(String[] args) { Account acct1 = new Account("666666",20); Account acct2 = new Account("123456",40); Account acct3 = new Account("987654",100); Account.setInterestRate(0.02); System.out.println("账户:"+ acct1.getId() +" 密码:"+ acct1.getPassword()+ " 余额:"+ acct1.getBalance()); System.out.println(acct1); System.out.println(acct2); System.out.println(acct3); System.out.println("利率:"+Account.getInterestRate()); System.out.println("最小余额:"+Account.getMinBalance()); } }
这篇关于static应用举例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升