刚才跟着尚硅谷的宋老师学了两个小例题,首先是第一个例题,第一个例题难度不大,主要是考察了封装性,逻辑还是比较清晰的。
Account类:
package OOPexer1; public class Account { private int id; private double balance; private double annualInterestRate; public Account(int id, double balance, double annualInterestRate) { this.id = id; this.balance = balance; this.annualInterestRate = annualInterestRate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } //取钱 public void withdraw(double amount) { if(balance0) { balance+=amount; System.out.println("成功存入"+amount); } } }
Customer类:
package OOPexer1; public class Customer { private String firstName; private String lastName; private Account account; public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } }
测试类:
package OOPexer1; public class CustomerTest { public static void main(String[] args) { Customer cust=new Customer("Jane","Smith"); Account acct=new Account(1000, 2000, 0.0123); cust.setAccount(acct);//客户的account由初始化的null赋值为acct的地址,指向定义的账户对象 cust.getAccount().deposit(100); cust.getAccount().withdraw(960); cust.getAccount().withdraw(2000); } }
输出结果:
成功存入100.0
成功取出960.0
余额不足,取款失败
第二个例子就有一点点小难度了,主要是因为用到了对象数组,对象数组在使用之前也需要提前声明开辟好空间。先把代码展示出来:
Account类:
package OOPexer2; public class Account { private double balance; public Account(double init_balance) { this.balance=init_balance; } public double getBalance() { return this.balance; } //存钱 public void deposit(double amt) { if(amt>0) { balance+=amt; System.out.println("存钱成功"); } } //取钱 public void withdraw(double amt) { if(balance>=amt) { balance-=amt; System.out.println("取钱成功"); }else { System.out.println("余额不足"); } } }
Bank类:
package OOPexer2; public class Bank { private Customer[] customer;//或者可以在这 private Customer[] customer=new Customer[10]; //可以用来存放多个客户的数组 private int numberOfCustomers;//记录客户个数 public Bank() { customer=new Customer[10];//数组要指定长度 } //添加客户 public void addCustomer(String f,String l) { Customer cust=new Customer(f,l); // customer[numberOfCustomers]=cust; // numberOfCustomers++; //或 customer[numberOfCustomers++]=cust; } //获取客户个数 public int getNumberOfCustomer() { return numberOfCustomers; } //获取指定位置上的客户 public Customer getCustomer(int index) { // return customer[index];这样写不太好 if(index>=0&&indexCustomer类:
package OOPexer2; public class Customer { private String firstName; private String lastName; private Account account; public Customer(String l,String f) { this.firstName=f; this.lastName=l; } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public Account getAccount() { return this.account; } public void setAccount(Account account) { this.account=account; } }测试类:
package OOPexer2; public class BankTest { public static void main(String[] args) { Bank bank=new Bank(); bank.addCustomer("Jane", "Smith"); System.out.println("当前账户人数为:"+bank.getNumberOfCustomer()); //System.out.println(bank.getCustomer(0)); //连续 *** 作 bank.getCustomer(0).setAccount(new Account(2000)); bank.getCustomer(0).getAccount().withdraw(500); double balance =bank.getCustomer(0).getAccount().getBalance(); System.out.println("账户余额为"+balance); bank.addCustomer("Tom", "Smith"); System.out.println("当前账户人数为:"+bank.getNumberOfCustomer()); } }首先明确一下其中的类 :银行类 、客户类、账户类。
在一个例子中银行对象一般只有一个,银行中有客户类,而此例中将客户类放在了数组中,方便对客户的增删改查,一个客户对象可能有多个账户对象 。所以整体上这三个类是逐个包含的关系。在内存中是逐个存储下一个的地址,指向下一个对象 。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)