/创建程序,在其中定义2个类,AccountTest和Account类体会java的封装性
//1.Account要求具有属性;姓名(长度2-4),余额(必须大于20),密码(必须是6位数),如果不满足,则给出提示信息,并给默认值
//2.通过setxxx方法给Account的属性赋值
//3.在AccountTest中测试
public class AccountTest {
public static void main(String[]args){
Account text=new Account("马启航",123643568,"030120");
Account text01=new Account("马启航gtdhdf",12,"0301206");
}
}
//创建程序,在其中定义2个类,AccountTest和Account类体会java的封装性
//1.Account要求具有属性;姓名(长度2-4),余额(必须大于20),密码(必须是6位数),如果不满足,则给出提示信息,并给默认值
//2.通过setxxx方法给Account的属性赋值
//3.在AccountTest中测试
class Account{
private String name;
private double balance;//余额
private String passwork;
//创建构造器
public Account(String name,double balance,String passwork){
setName(name);
setBalance(balance);
setPasswork(passwork);
}
public String getPasswork() {
return passwork;
}
public void setPasswork(String passwork) {
if(passwork.length()==6){
this.passwork = passwork;
}else {
System.out.println("请输入正确的密码,密码为6位数,否则密码为默认值");
this.passwork="123456";
}
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
if (balance>20) {
this.balance = balance;
}else{
System.out.println("余额必须大于20,否则余额为0");
this.balance=0;
}
}
public String getName() {
return name;
}
public void setName(String name) {
if (name.length()>=2&&name.length()<=4){
this.name = name;
}else {
System.out.println("请输入正确的名字,名字长度在2-4字符之间,否则名字变为匿名");
this.name="匿名";
}
}
}