代码如下:
package Scaladay07
object Demo82 {
def main(args: Array[String]): Unit = {
//开卡
val account = new Account("622358711252255",8920.4,"123456")
account.query("123456")
account.withDreaw("123456",100.0)
account.query("123456")
}
}
//编写一个Account类
class Account(inAccount:String,inBalance:Double,inPwd:String){
val accountNO = inAccount
var balance = inBalance
var pwd = inPwd
//查询
def query(pwd: String):Unit ={
if (!this.pwd.equals(pwd)){
println("密码错误")
return
}
printf("账号为:%s 当前余额是:%.2f\n",this.accountNO,this.balance)
}
//取款
def withDreaw(pwd: String,money:Double):Any = {
if (!this.pwd.equals(pwd)){
println("密码错误")
return
}
//判断money是否合理
if (this.balance < money){
println("余额不足")
return
}
this.balance -= money
money
}
}
结果: