java的super关键字的使用


文章内容选自尚硅谷

super关键字的一般用法

super关键字可用于可用于解决子父类出现同名属性的情况,用super关键字来声明某个属性是父类的属性。

  • super关键字意思为“父类的”。
  • super关键字可以用来调用属性、方法、构造器。
  • 当子父类出现同名的属性,super可声明该属性是父类的属性。
    假如先创建了一个Person类
package com.atguigu.java3;

public class Person {
    
    
	String name;
	int age;
	int id = 1001;//身份证号
	
	public Person(){
    
    
		
	}
	public Person(String name){
    
    
		this.name = name;
	}
	public Person(String name,int age){
    
    
		this(name);
	
		this.age = age;
	}
	
	public void eat(){
    
    
		System.out.println("人吃饭");
	}
	
	public void walk(){
    
    
		System.out.println("人走路");
	}
}

再创建一个Student类继承Person类

package com.atguigu.java3;

public class Student extends Person{
    
    
	String major;
	int id = 1002;//学号
	
	public Student(){
    
    
		
	}
	public Student(String major){
    
    
		this.major = major;
	}
	
	@Override
	public void eat() {
    
    
		System.out.println("学生吃饭");
		
	}
	
	public void study(){
    
    
		System.out.println("学生学习");
	}
	
	public void show(){
    
    
		System.out.println("name="+ this.name+",age="+super.age);
		System.out.println("id="+id);
		System.out.println("id="+super.id);
	}
}

再在SuperTest类中进行测试

package com.atguigu.java3;

public class SuperTest {
    
    
	public static void main(String[] args) {
    
    
		Student s = new Student();
		s.show();
	}
}

测试结果为

name=null,age=0
id=1002
id=1001

在Student类中

		System.out.println("name="+ this.name+",age="+super.age);
		System.out.println("id="+id);
		System.out.println("id="+super.id);
  • id其实是省略掉了默认的this.关键字。
  • 在this.name中,由于属性是不能够重写的,先在本Student类中去找name属性,找不到再到父类中去找
  • 在super.age中,直接到父类中去找。

super关键字修饰方法

类中的方法默认都是this.来修饰的,加上super.表示的是调用父类中的方法,通常用于区别重写的方法和被重写的方法。
假如在Student类中加入

	@Override
	public void eat() {
    
    
		System.out.println("学生吃饭");
		
	}
	
	public void study(){
    
    
		System.out.println("学生学习");
		eat();
		super.eat();
	}

测试

package com.atguigu.java3;

public class SuperTest {
    
    
	public static void main(String[] args) {
    
    
		Student s = new Student();
//		s.show();
		s.study();
	}
}

结果为

学生学习
学生吃饭
人吃饭

  • this.方法 和 super.方法 一样,加上this,现在本类中找方法,找不到再到父类中去找;而super直接在父类中去找。
  • super关键字不仅要求在直接父类中去找方法,还要求在间接父类中去寻找方法。

super调用构造器

super调用构造器和this调用构造器很类似,都可以有效地避免代码的冗余,还能够有效地避免父类私有属性的影响。

比如在Student类中将构造器改为

	public Student(){
    
    
		
	}
	public Student(String major){
    
    
		this.major = major;
	}
	
	public Student(String name,int age,String major){
    
    
		super(name,age);
		this.major = major;
	}

就调用了Person类中的

	public Person(String name,int age){
    
    
		this(name);
	
		this.age = age;
	}

测试代码

		Student s1 = new Student("tom",21,"automation");
		s1.show();

运行结果为

name=tom,age=21
id=1002
id=1001

这样不仅能够避免代码冗余,当Person类中的name和age为private的时候,采用super调用的方法,能够不影响Person类的封装性。

  • super(形参列表)用于在子类中调用父类声明的构造器
  • super(形参列表)必须声明在子类构造器的首行
  • 类的构造器中,this(形参列表)和super(形参列表)只能二选一。
  • 如果构造器中 this(形参列表)和super(形参列表) 都没有用,其实在首行默认用了super()
    假如把Person类中的构造器
	public Person(){
    
    
		System.out.println("everywhere");
	}

测试代码

package com.atguigu.java3;

public class SuperTest {
    
    
	public static void main(String[] args) {
    
    
		Student s1 = new Student();
		
	}
}

运行结果

everywhere

这是因为Student构造器中

	public Student(){
    
    
		
	}
	public Student(String major){
    
    
		this.major = major;
	}
	
	public Student(String name,int age,String major){
    
    
		super(name,age);
		this.major = major;
	}

其实默认等同于

	public Student(){
    
    
		super();
	}
	public Student(String major){
    
    
		super();
		this.major = major;
	}
	
	public Student(String name,int age,String major){
    
    
		super(name,age);
		this.major = major;
	}

ps:因为在this关键字的使用时讲到,一个类如果有n个构造器,最多有n-1个this(形参列表),可以推断出类的多个构造器中,至少有一个类的构造器中使用了super(形参列表)调用了父类的构造器。

猜你喜欢

转载自blog.csdn.net/Meloneating/article/details/113755650