先设置一个功能类,实现存取款的功能,其中设置public bake(double money, double yuge) {
super();
this.money = money;
this.yuge = yuge;
}
构造方法,就是为啦在创建对象的时候可以赋值。
里面的yuge变量就是记录余额还有多少,momey就是记录输入的钱数,因为我只需要获取余额,所以只创建啦getYuge(),取款和存款的方法分别如下:
public class bake {
public double money;
public double yuge;
public bake() {
super();
// TODO Auto-generated constructor stub
}
public bake(double money, double yuge) {
super();
this.money = money;
this.yuge = yuge;
}
//获取余额
public double getYuge() {
return yuge;
}
//存款
public void cunkuan(double i) {
yuge+=i;
}
//取款
public void qukuan(double j) {
yuge -= j;
}
}
接下来就是测试类:
public class ceshilei {
public static void main(String[] args) {
// TODO Auto-generated method stub
bake str=new bake(500,2000);
str.qukuan(30);
System.out.println("取款后的余额为:"+str.getYuge());
str.cunkuan(600);
System.out.println("取款后的余额为:"+str.getYuge());
}
}