21-03-16super关键字的使用

本文详细介绍了Java中的super关键字,包括如何使用super调用父类的属性、方法和构造器。特别强调了在子类中调用父类同名属性及重写方法时的注意事项,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

super关键字的使用

  • super理解为:父类的

  • super可以调用

    属性,方法,构造器

  • super调用属性和方法

    1. 在子类的方法或构造器中,通过使用"super.属性"或"super.方法"的方式,显式的调用父类中声明的属性或方法,但是在通常情况下,我们习惯性省略"super."
    2. 特殊情况:当子类和父类中定义了同名的属性时,我们想要在子类中调用父类中声明的属性,则必须显式的使用"super.属性"的方式,来表明调用的是父类中声明的属性。
    3. 特殊情况:当子类重写了父类中的方法以后,我们想要在子类的方法中调用父类中被重写的方法时,则必须显式的使用"super.方法"的方式,表明调用的是父类中被重写的方法。
super.withdraw(amount);
  • super调用构造器
    1. 可以在子类的构造器中显式的使用"super(形参列表)"的方式,调用父类中声明的指定的构造器。
    2. "super(形参列表)"的使用,必须声明在子类构造器的首行。
    3. 我们在类的构造器中,针对于"this(形参列表)"或"super(形参列表)"只能二选一,不能同时出现
    4. 在构造器的首行,没有显示的声明"this(形参列表)“或"super(形参列表)”,则默认调用的是父类中空参的构造器。若父类中只声明了带参的构造器,则会因无空参构造器而报错。
    5. 在类的多个构造器中,至少有一个类的构造器中使用了"super(形参列表)",调用父类中的构造器。
  • 以下未测试的代码
package 继承性测试.继承和super练习;

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 annualInterestRate/12;
	}
	public void withdraw (double amount){
		if(amount <= balance){
			balance -= amount;
			System.out.println("取钱成功:"+amount);	
		}
		else
			System.out.println("取钱失败");	
		
	}
	public void deposit (double amount){
		if(amount <= 0) return;
		balance += amount;
		System.out.println("存钱成功:"+balance);	
	}	
}
package 继承性测试.继承和super练习;

public class AccountTest {
	public static void main(String[] args) {
		Account account = new Account(1122, 20000, 0.045);
		account.withdraw(30000);
		System.out.println("余额为:" + account.getBalance());
		account.withdraw(2500);
		System.out.println("余额为:" + account.getBalance());
		account.deposit(3000);
		System.out.println("余额为:" + account.getBalance());
	}
}
package 继承性测试.继承和super练习;

public class CheckAccount extends Account {
	double overdrafit;
	

	public CheckAccount(int id, double balance, double annualInterestRate, double overdrafit){
		super(id, balance, annualInterestRate);
		this.overdrafit = overdrafit;
	}
	
	public void withdraw (double amount){
		if(amount <= getBalance()){

//			setBalance(getBalance() - amount);
			super.withdraw(amount);
		}
		else{
			if(overdrafit >= amount- getBalance()){
				overdrafit -= (amount- getBalance());
				setBalance(0);
			}
			else System.out.println("超过可透支的金额");
		}
		

	}
	
	public double getOverdrafit() {
		return overdrafit;
	}

}
package 继承性测试.继承和super练习;

public class CheckAccountTest {
	public static void main(String[] args) {
		CheckAccount ca = new CheckAccount(1122,20000,0.045,5000);
		ca.withdraw(5000);
		System.out.println("余额"+ca.getBalance());
		ca.withdraw(18000);
		System.out.println("余额"+ca.getBalance());
		System.out.println("额度"+ca.getOverdrafit());
		ca.withdraw(3000);
		System.out.println("余额"+ca.getBalance());
		System.out.println("额度"+ca.getOverdrafit());
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值