java 关键字super

版权声明:尊重原创,码字不易,转载需博主同意。 https://blog.csdn.net/qq_34626097/article/details/83021195

java 关键字super

java关键字的使用

  1. super:可用来修饰属性、方法、构造器

  2. 当子类与父类中有同名的属性时,可用通过“super.此属性”显示调用父声明的属性。

  3. 若想调用子类的同名的属性“用this.属性”调用

  4. 当子类重写父类的方法以后,在子类中若想调用父类的方法,就需要显示调用父类中的方法,用“super.方法”

demo

public class Person {

	private String name;
	private int age;
	int id;//身份证
	...
	public void eat() {
		System.out.println("吃饭");
	}
}
public class Student extends Person {
	private String schoolName;
	private int id;//学号
	public Student(String schoolName, int id) {
		super();//调用父类的构造器结构,加载父类的属性
		this.schoolName = schoolName;
		this.id = id;
	}
	
	public void show() {
		//默认this.id
		System.out.println(this.id);		
		System.out.println(super.id);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_34626097/article/details/83021195