一:请通过代码吗封装,实现如下需求:
编写一个类book,代表教材:
1.具有属性:名称(title) 页数(pageNum)
2.其中页数不能少于200页,否则输出错误信息,并赋予默认值200
3.为各属性提供赋值和赋值方法
4.具有方法:detail,用来在控制台输出每本教材的名称和页数
5.编写测试类BookTest进行测试:为Book对象的属性赋予初始值,并调用Book对象的detail
public class Homework01{
public static void main(String[] args){
//创建Book对象
Book b1 = new Book("高三数学人教版",250);
//调用detail方法
b1.detail();
//修改页数
b1.setPageNum(100);
b1.detail();
Book b2 = new Book();
b2.detail();
}
}
class Book{
//名称
private String title;
//页数
private int pageNum;
//构造方法,无参数
public Book(){
title = "未知";
pageNum = 200;
}
//有参数构造器
public Book(String s,int i){
title = s;
if(i < 200){
pageNum = 200;
System.out.println("页数不能少于200,少于200时,默认为200页");
}else{
pageNum = i;
}
}
//setter and getter方法
public void setTitle(String s){
title = s;
}
public String getTitle(){
return title;
}
public void setPageNum(int i){
if (i <200){
System.out.println("本书页数不能少于200页,默认赋值200");
//默认赋值200
pageNum = 200;
return;
}
//说明页数是大于等于200的
pageNum = i;
}
public int getPageNum(){
return pageNum;
}
//额外再提供一个detail方法
public void detail(){
// System.out.println("教材名称:" + this.title + "总页数:" + this.pageNum);
//this.可以省略的
System.out.println("教材名称:" + title +" "+ "总页数:" + pageNum);
}
}
二.写一个类名为Account的模拟账户
该类的属性和方法如下所示.
该类包括的属性:账户id,余额balance,年利率annualInterestRate;
包含的方法:各属性的set和get方法,取款方法withdraw().存款方法deposit()
写一个测试程序
(1)创建一个Customer,名字叫Jane Smith,他有一个账号为1000,余额为2000,年利率为1.23%
存入100元,再取出960元,再取出2000
打印Jane Smith的基本信息
信息如下显示:
成功存入: 100
成功取出: 960
余额不足:取钱失败
Customer [Smith, jane] has a account:id is 1000 annualInterestRate is 1.23% balance
public class Homework02{
public static void main(String[] args){
//先创建一个账户对象
Account a = new Account("1000",2000,1.23);
//创建客户对象
Customer c = new Customer("Jane Smith",a);
/*
对Jane Smith操作:
存入100元,
再取出960元,再取出2000
*/
c.getAccount().deposit(100);
c.getAccount().withdraw(960);
c.getAccount().withdraw(2000);
//银行卡是不是应该有一个主人
//此程序最难的地方是:发现对象之间的关联,不好发现
}
}
//以后都是封装,所有的类都是属性私有化,对外提供setter and getter方法
//客户类
class Customer{
//客户名字
private String name;
//客户手里应该有银行账户
private Account account;
//构造方法
public Customer(){
}
public Customer(String name,Account account){
this.name = name;
this.account = account;
}
//setter and getter方法
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAccount(Account account){
this.account = account;
}
public Account getAccount(){
return account;
}
}
//账户类
class Account{
//账户id
private String id;
//余额
private double balance;
//年利率
private double annualInterestRate;
//构造方法
public Account(){
}
public Account(String id,double balance,double annualInterestRate){
//创建对象时所需要的代码
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
//setter and getter
//为什么要写set和get方法,用得着吗?用不着你也得写,因为这是"封装"规定的
public void setId(String id){
//对象创建完之后,想修改id属性的调用此方法
this.id = id;
}
public String getId(){
return id;
}
// 实例方法(需要"引用."来调用)
public void setBalance(double balance){ //修改余额的方法
this.balance = balance;
}
public double getBalance(){
return balance;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
//存款方法
public void deposit(double money){
//this(向this指向的对象中存款)
// this.balance = this.balance +money;
// this.balance += money;
//如果省略this
// balnace = balance +money;
// balance += money;
//调用方法来进行修改余额
this.setBalance(this.getBalance()+money);
this. 可以省略
// setBalance(getBalance()+money);
System.out.println("成功存入:" + money);
}
//取款方法
//调用取钱的方法时,应该传递过来一个参数,告诉该方法要取多少钱
public void withdraw(double money){
//this(从this指向的对象中取款)
if (money > this.getBalance()){
System.out.println("余额不足,取钱失败");
return;
}
//程序能够执行到此处说明余额不足
this.setBalance(this.getBalance()-money);
System.out.println("成功取出:" + money);
}
}
1.属性私有化,
2.提供无参有参构造,
3.提供get,set方法,
难点:
1.此程序最难的地方是:发现对象之间的关联,不好发现
2.另外就是取款,取款方法的编写