JavaSE--super关键字

发布时间 2023-08-09 10:40:52作者: 洛小依ovo

一、super关键字

1、super

是一个关键字,全部小写

2、this与super对比

  this:

  •     this可以出现在实例方法中和构造方法中
  •     语法:this.或者this()
  •     this不能使用在静态方法中
  •     this.大部分可以省略,在区分局部变量和实例变量的时候不能省略,
  •     this()调用本类中其他构造方法,只能出现在第一行

  super:

  •     super可以出现在实例方法中和构造方法中
  •     语法:super.或者super()
  •     super不能使用在静态方法中
  •     super.大部分可以省略,什么时候可以省略
  •     super()调用父类中其他构造方法,只能出现在第一行,目的是:创建子类对象时,先初始化父类特征

3、重要结论:

  当一个构造方法第一行:既没有this()又没有super()的话,默认会有一个super();

  表示通过当前子类的构造方法调用父类的无参构造方法

  所以必须保证父类的无参构造时存在的

4、注意:

  this()和super()不能共存,他们都只能出现在构造方法的第一行

5、无论如何怎么样,父类的构造方法时一定会执行的,子类构造方法执行之前必然调用父类的构造方法

public class SuperTest01{
    public static void main(String[] args){
        // 创建子类对象
        new B();// 输出:A类中的无参构造  B类中的无参构造
    }
}

class A{
    // 建议将一个类的无参构造手动写出来
    public A(){
        System.out.println("A类中的无参构造");
    }
    public A(int i){
        System.out.println("A类中的有参构造(int)");
    }
}

class B extends A{
    public B(){
        // super();// 规定:如果构造方法中什么都不写,第一行自动会有super();
        super(100);
        System.out.println("B类中的无参构造");
    }
}

 二、什么时候使用super(实际参数列表);

  注意:在构造方法执行过程中一连串调用了父类的构造方法,父类的构造方法又继续向下调用它的父类的构造方法,但是实际上对象只创建了一个

  super(实际参数列表);的作用:初始化当前对象的父类型特征,不是来创建对象的

  super关键字就代表了“当前对象”的那部分父类型特征

public class SuperTest03{
	public static void main(String[] args){

		CreditAccount ca1 = new CreditAccount();
		System.out.println(ca1.getActno() + "," + ca1.getBalance() + "," + ca1.getCredit());

		CreditAccount ca2 = new CreditAccount("1111", 10000.0, 0.999);
		System.out.println(ca2.getActno() + "," + ca2.getBalance() + "," + ca2.getCredit());

	}
}

// 账户
class Account extends Object{
	// 属性
	private String actno;
	private double balance;

	// 构造方法
	public Account(){
		//super();
		//this.actno = null;
		//this.balance = 0.0;
	}
	public Account(String actno, double balance){
		// super();
		this.actno = actno;
		this.balance = balance;
	}

	// setter and getter
	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(String actno, double balance, double credit){

		// 私有的属性,只能在本类中访问。
		/*
		this.actno = actno;
		this.balance = balance;
		*/

		// 以上两行代码在恰当的位置,正好可以使用:super(actno, balance);
		// 通过子类的构造方法调用父类的构造方法。
		super(actno, balance);
		this.credit = credit;
	}

	// 提供有参数的构造方法
	public CreditAccount(){
		//super();
		//this.credit = 0.0;
	}

	// setter and getter方法
	public void setCredit(double credit){
		this.credit = credit;
	}
	public double getCredit(){
		return credit;
	}
	
}

 

三、什么时候super.不能省略

  如果父类和子类中有同名属性,并且想要在子类中访问父类中的同名属性,suer.不能省略

  如果父类和子类中有相同方法,此时想要在子类中访问父中的数据,必须使用suer.区分

四、结论

  super 不是引用,super也不保存内存地址,也不指向任何对象

  super只代表当前对象内部那一块父类型的特征

  super不仅可以访问属性,也可以访问方法