Java关键字——super
(1)super理解为:父类的
(2)super可以用来调用:属性、方法、构造器
(3)super的使用:(调用属性、方法)
①我们可以在子类的方法或构造器中,通过使用“super.属性”或“super.方法”的方式,显式的调用父类中声明的属性或方法。但是,通常情况下,我们习惯省略“super”;
②特殊情况:当子类和父类中定义了同名的属性时,我们想要在子类中调用父类中声明的属性,则必须显式的使用“super.属性”的方式,表明调用的是父类中声明的属性;
③特殊情况:当子类重用了父类中的方法以后,我们想在子类的方法中调用父类中被重写的方法时,则必须显式的使用“super.方法”的方式,表明调用的是父类中被重写的方法;
(4)super调用构造器
①我们可以在子类的构造器中显式的使用“super(形参列表)”的方式,调用父类中声明的指定的构造器;
②“super(形参列表)”的使用,必须声明在子类构造器的首行;
③在类的构造器中,针对于“this(形参列表)”或“super(形参列表)”只能二选一,不能同时出现;
④在构造器的首行,没有显式的声明“this(形参列表)”或“super(形参列表)”,则默认调用的是父类中空参的构造器:super();
⑤在类的多个构造器中,至少有一个类的构造器中使用了“super(形参列表)”,调用父类中的构造器。
下面的程序由三个类组成:(1)Account;(2)CheckAccount;(3)AccountTest。
其中,①CheckAccount类是Account的子类;②Account类中重写了CheckAccount类中的取款方法:withdraw();③CheckAccount类中的构造器使用了super(形参列表)的形式调用了父类中的构造器;④也通过“super.”的方式调用了父类中的属性与方法。
另外,withdraw()方法的重写部分,还有两种方法可以用,一种是修改父类中balance的权限,另外一种是通过super.withdraw(money)的方式调用。如果没有在父类中声明setBalance(double balance)的方法,那就只能通过这两种方式来做。在实际的项目中,余额其实是不能够通过set方法来改变的,只能通过withdraw()取款的形式改变,这里我是为了展示this关键字和super关键字两者之间的相互使用,我就用的setBalance来获取的余额。为了搞清楚调用的是哪个类的属性和方法,我的super和this关键字都没有进行省略,更方便搞清楚调用属性、方法和构造器的逻辑性。
package com.java.oop3;
/**
* @ClassName Account
* @Description: 模拟账户
* @Author Seven
* @Date 2021/7/29 15:13
* @Version 1.0
**/
public class Account {
private int id; //账号
private double balance; //余额
private double annualInterestRate; //年利率
public Account(){ //这个是空参的构造器
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
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;
}
public double getMonthlyInterest(){ //返回月利率
return this.getAnnualInterestRate()/12;
}
public void withdraw(double money){ //取款
if(this.balance - money < 0){
System.out.println("余额不足!您的余额为:"+this.balance);
}else{
this.balance -= money;
System.out.println("取款成功!取款金额为:"+money+"元,您的余额为:"+this.balance);
}
}
public void deposit(double money){ //存款
if(money > 0){
this.balance += money;
System.out.println("存款成功!存款金额为:"+money+"元,您的余额为:"+this.balance);
}
}
}
package com.java.oop3;
/**
* @ClassName CheckAccount
* @Description: 可透支账户
* @Author Seven
* @Date 2021/7/29 15:28
* @Version 1.0
**/
public class CheckAccount extends Account{
private double overdraft;
public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
super(id, balance, annualInterestRate);
this.overdraft = overdraft;
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
public void withdraw(double money){ //取款
//方法一:
if(super.getBalance() - money < 0){
double result = money - super.getBalance();
if(this.getOverdraft() > result){
super.setBalance(0);
this.setOverdraft(getOverdraft() - result);
System.out.println("取款成功!您的余额为:"+super.getBalance()+"元,您还可以透支金额为:"+this.getOverdraft());
}else{
System.out.println("您超过可透支额的限额!您的余额为:"+super.getBalance()+",你还可以透支金额为:"+this.getOverdraft());
}
}else{
super.setBalance(getBalance() - money);
System.out.println("取款成功!取款金额为:"+money+"元,您的余额为:"+super.getBalance()+"您还可以透支金额为:"+this.getOverdraft());
}
}
//方法二:
//通过super.withdraw(money)
//方法三:
//通过修改balance的权限,将private改成protect
}
package com.java.oop3;
/**
* @ClassName AccountTest
* @Description: 测试类
* @Author Seven
* @Date 2021/7/29 15:23
* @Version 1.0
**/
public class AccountTest {
public static void main(String[] args) {
Account account = new Account(1122,20000,0.045);
account.withdraw(30000);
account.deposit(3000);
System.out.println("月利率为:"+account.getMonthlyInterest());
CheckAccount checkAccount = new CheckAccount(1122,20000,0.045,5000);
checkAccount.withdraw(5000);
checkAccount.withdraw(18000);
checkAccount.withdraw(3000);
}
}