super关键字调用方法/属性的使用:
1 super 可以用来调用:属性,方法,构造器
2 super我们可以调用父类中的方法或属性,即使该方法被子类重写,也是可以调用的
语法:super.方法/属性
父类:
package 继承;
public class Person extends Creature{
String name;
int age;
String id = "12345678910"; //身份证号
public Person(){}
public Person(String name,int age) {
this.name=name;
this.age=age;
}
public String eat() {
return "吃饭";
}
public void sleep(){
System.out.println("睡觉");
}
protected String showId() {
return this.id;
}
}
子类:
package 继承;
public class Student extends Person{
// String name;
// int age;
String major;
String id = "2018450800"; //学号
public Student() {}
public Student(String major) {
this.major=major;
}
public void study() {
System.out.println("学习");
}
// 重写父类中的eat()
public String eat() {
return "我是学生,我吃食堂";
}
// 重写showId函数
public String showId() {
return this.id;
}
protected void show1() {
// 通过 super我们可以调用已经被重写过的父类中的方法或属性
System.out.println("id_st:"+this.showId()+"\nid_per:"+super.showId());
}
protected void show2() {
System.out.println("Stu:"+this.eat()+"\nPer:"+super.eat());
}
}
main:
package 继承;
/**
* @Description super关键字调用方法/属性
* @author HuXuehao Email:
* @version
* @date 2020年8月10日上午8:54:02
*/
/**
* super关键字调用方法/属性的使用:
* 1. super 可以用来调用:属性,方法,构造器
* 1.2.super我们可以调用父类中的方法或属性,即使该方法被子类重写,也是可以调用的
* 语法:super.方法/属性
*/
public class super_use {
public static void main(String[] args) {
Student student = new Student();
student.show1();
student.show2();
}
}