实验程序如下:
package 银行账户;
public class Account
{
private String acnumber;//账号
private String name;//储户姓名
private String khtime;//开户时间
private String accId;//身份证号
private double money;//存款余额
private double Cmoney;//存入的钱
private double Qmoney;//取出的钱
public Account (String acnumber,String name,String khtime,String accId,double money,double Cmoney,double Qmoney){
this.acnumber=acnumber;
this.name = name;
this.khtime=khtime;
this.accId = accId;
this.money = money;
this.Cmoney = Cmoney;
this.Qmoney = Qmoney;
}//构造函数,初始化
public void cunkuan()
{ if(Cmoney <= 0){
System.out.println("存款金额必须大于0");}
else
this.money += Cmoney;
System.out.println("存款成功!");
System.out.println("存款后的余额为:"+this.money);
}//取款
public double qukuan()
{if(Qmoney <= 0){
System.out.println("取款金额必须大于0");
return 0;
}
if(this.money <= Qmoney){
System.out.println("余额不足,无法取款");
return 0;
}
else
this.money -= Qmoney;
System.out.println("取款成功!");
System.out.println("取款后的余额为:"+this.money);
return money;
}//取款
public void chaxun()
{
System.out.println("查询成功!");
System.out.println("账户余额为"+this.money);
}//查询
public void finalize()
{
System.out.println("Destructor called!");
}//析构函数,释放空间
public static void main(String[] args) {
Account a1 = new Account("3485658","Dali","2019-4-11","123456789",2500,4565,5878);
a1.cunkuan();
a1.qukuan();
a1.chaxun();
a1.finalize();
}
}
{
private String acnumber;//账号
private String name;//储户姓名
private String khtime;//开户时间
private String accId;//身份证号
private double money;//存款余额
private double Cmoney;//存入的钱
private double Qmoney;//取出的钱
public Account (String acnumber,String name,String khtime,String accId,double money,double Cmoney,double Qmoney){
this.acnumber=acnumber;
this.name = name;
this.khtime=khtime;
this.accId = accId;
this.money = money;
this.Cmoney = Cmoney;
this.Qmoney = Qmoney;
}//构造函数,初始化
public void cunkuan()
{ if(Cmoney <= 0){
System.out.println("存款金额必须大于0");}
else
this.money += Cmoney;
System.out.println("存款成功!");
System.out.println("存款后的余额为:"+this.money);
}//取款
public double qukuan()
{if(Qmoney <= 0){
System.out.println("取款金额必须大于0");
return 0;
}
if(this.money <= Qmoney){
System.out.println("余额不足,无法取款");
return 0;
}
else
this.money -= Qmoney;
System.out.println("取款成功!");
System.out.println("取款后的余额为:"+this.money);
return money;
}//取款
public void chaxun()
{
System.out.println("查询成功!");
System.out.println("账户余额为"+this.money);
}//查询
public void finalize()
{
System.out.println("Destructor called!");
}//析构函数,释放空间
public static void main(String[] args) {
Account a1 = new Account("3485658","Dali","2019-4-11","123456789",2500,4565,5878);
a1.cunkuan();
a1.qukuan();
a1.chaxun();
a1.finalize();
}
}
实验结果如下:
实验心得:
1.Java中类的封装是面向对象的核心特性,是信息隐蔽思想的具体实现技术,感觉和C++中类的封装有很多相似的地方。
2.一开始拿到程序,无从下手,仔细想后,无非就是将银行客户的私人信息隐蔽,再就是对钱的操作,就存和取。
3.此程序还应考虑到存的钱大于零,账户里的钱大于取的钱。