继承的代码解释(后)
作者:互联网
//使用继承机制来解决代码复用问题。
public class ExtendsTest02 {
public static void main(String[] args) {
//创建普通账户
Account act=new Account();
act.setActno("111111");
act.setBalance(10000);
System.out.println(act.getActno()+",余额"+act.getBalance());
//创建信用账户
CreditAccount ca=new CreditAccount();
ca.setActno("222222");
ca.setBalance(-10000);
ca.setCredit(0.99);
System.out.println(ca.getActno()+",余额"+ca.getBalance()+",信誉度"+ca.getCredit());
}
}
//银行账户类
//账户的属性:账号、 余额
class Account{ //父类
//属性
private String actno;
private double balance;
//构造方法
public Account(){
}
public Account(String actno,double balance){
this.actno=actno;
this.balance=balance;
}
//setter and getteer方法
public void setActno(String actno){
this.actno=actno;
}
public String getActno(){
return actno;
}
public void setBalance(double balance){
this.balance=balance;
}
public double getBalance(){
return balance;
}
}
//其它类型的账户:信用卡账户
//账号、余额、信誉度
class CreditAccount extends Account{//子类
//属性
private double credit;
//构造方法
public CreditAccount(){
}
//setter and getter 方法
public void setCredit(double credit){
this.credit=credit;
}
public double getCredit(){
return credit;
}
}
标签:解释,Account,继承,double,代码,ca,balance,actno,public 来源: https://www.cnblogs.com/yyn1999/p/16259938.html