Java中super关键字与继承结合的使用
super关键字
super: 在java中表示父类的,可以用来调用属性、方法、构造器。
调用属性、方法:
super.属性,super.方法。
当子类和父类中定义了同名的属性时,使用"super.属性",表明调用的是父类中声明的属性。
当子类重写了父类中的方法以后,使用"super.方法",表明调用的是父类中声明的方法。
调用构造器:
super()调用父类中空参构造器。
super(形参) 调用父类中指定构造器。
继承
使用关键字extends,例如A类继B类
public class A extends B{
}
举例:银行信用卡账户
1. 写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器方法(getter 和setter 方法),返回月利率的方法 getMonthlyInterest(),取款方法 withdraw(),存款方法deposit()。
2. 创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法。
要求: 写一个用户程序测试 CheckAccount 类。在用户程序中,创建一个账号为 1122、余额为 20000年利率 4.5%,可透支限额为 5000 元的 CheckAccount 对象。使用 withdraw 方法提款 5000 元,并打印账户余额和可透支额。再使用 withdraw 方法提款 18000 元,并打印账户余额和可透支额。再使用 withdraw 方法提款 3000 元,并打印账户余额和可透支额。
创建Account类
public class Account {
private int id;//账号
private double balance;//余额
private double annualInterestRate;//年利率
//构造器
public Account(int id, double balance, double annualInterestRate) {
super();//super关键字,默认调用Object类的构造器。
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
//取款方法 withdraw()
public void withdraw(double amount){
if(balance >= amount){
balance -= amount;
return;
}
System.out.println("余额不足!");
}
//存款方法deposit()
public void deposit(double amount){
if(amount > 0){
balance += amount;
}
}
//返回月利率
public double getMonthlyInterest(){
return annualInterestRate / 12;
}
//get和set方法,eclipse中可以使用快捷键alt+shift+s快速得到get和set方法。
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
}
创建CheckAccount类
public class CheckAccount extends Account{//体现了继承关系,CheckAccount 为子类,Account为父类
super(id,balance,annualInterestRate);//用super(形参);
//因为父类中的属性为private权限,所以用set方法。
// this.setId(id);
// this.setBalance(balance);
// this.setAnnualInterestRate(annualInterestRate);
this.overdraft = overdraft;
}
@Override
public void withdraw(double amount) {
if(amount <= getBalance()){//余额就足够消费
// getBalance() -= amount; //错误写法
//方式一:有时银行不提供set方法
// setBalance(getBalance() - amount);
//方式二:使用super.方法调用父类withdraw, 如果没有suoer.就会调用自己进入循环。
super.withdraw(amount);
//方式三:修改balance权限为protected,让子类可以看到。
}else if(overdraft >= amount - getBalance()){//透支额度+余额足够消费
overdraft -= amount - getBalance();
//方式一:使用set设置余额为0
// setBalance(0);
//方式二:使用super.方法设置余额为0
super.withdraw(getBalance());
}else{
System.out.println("超过可透支限额!");
}
//另外一种方法
// else{
// double requireOverdraft = amount - getBalance();
// if(requireOverdraft <= overdraft){
// setBalance(0);
// overdraft -= requireOverdraft;
// }else{
// System.out.println("超过可透支额的限额!");
// }
// }
}
//get和set方法:前期学的时候建议自己写一下
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
}
创建CheckAcoountTest类
public class CheckAcoountTest {
public static void main(String[] args) {
//对象实例化
CheckAccount account = new CheckAccount(1122, 20000, 0.045, 5000);
account.withdraw(5000);
System.out.println("您的账户余额为:" + account.getBalance());
System.out.println("您的可透支余额为:" + account.getOverdraft());
account.withdraw(18000);
System.out.println("您的账户余额为:" + account.getBalance());
System.out.println("您的可透支余额为:" + account.getOverdraft());
account.withdraw(3000);
System.out.println("您的账户余额为:" + account.getBalance());
System.out.println("您的可透支余额为:" + account.getOverdraft());
}
}
运行结果
您的账户余额为:15000.0
您的可透支余额为:5000.0
您的账户余额为:0.0
您的可透支余额为:2000.0
超过可透支限额!
您的账户余额为:0.0
您的可透支余额为:2000.0
综上 ,上述例子可以很好地体会封装,继承,this关键字和super关键字的使用。
参考 https://www.bilibili.com/video/BV1Kb411W75N/p=279&spm_id_from=pageDriver&vd_source=e3d2dcdad07ba1c727f3c9e7654f60b5