实验内容:
public class Account {
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.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("123456","Wfx","19-04-14","123321",1234,5678,9123);
a1.cunkuan();
a1.qukuan();
a1.chaxun();
a1.finalize();
}
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.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("123456","Wfx","19-04-14","123321",1234,5678,9123);
a1.cunkuan();
a1.qukuan();
a1.chaxun();
a1.finalize();
}
}
实验结果:
实验心得:类包括成员变量和方法,成员变量和方法统称为对象,方法中包括局部变量和其他方法。变量具有私有性,每个类的成员变量对其他类是私有的,方法中的局部变量对该类中的其他方法是私有的。