Java-继承-Super
时间:2020-02-23 14:36:38 来源:igfitidea点击:
在本教程中,我们将学习Java编程语言中的super关键字。
在先前的教程中,我们了解了继承以及如何访问继承的变量和方法。
随时检查一下。
Super关键字
在两种情况下,我们使用关键字" super"。
从子类中调用父类的构造函数。
访问隐藏在子类中的父类的成员。
调用父类的构造函数
为了从子类中调用父类的构造函数,我们使用" super"关键字,然后传递参数列表(如果有)。
语法:
class Parent {
//constructor
Parent() {
//some code...
}
}
class Child extends Parent {
//constructor
Child() {
super();
//some code...
}
}
" super()"语句必须是子构造函数中调用父构造函数的第一条语句。
例1:访问父构造函数
在下面的示例中,我们有父类" Person"和子类" Employee"。
从子类内部,我们正在调用父类的构造函数。
//the parent class
class Person {
//general member variables
String firstname;
String lastname;
//constructor
Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
//general method
public void showName() {
System.out.println("First name: " + this.firstname);
System.out.println("Last name: " + this.lastname);
}
}
//the child class inheriting the parent class
class Employee extends Person {
//specific member variables
String employeeid;
int payscale;
String joiningDate;
//constructor
Employee(String firstname, String lastname, String employeeid, int payscale, String joiningDate) {
//calling the constructor of the parent class Person
//to set the first name and last name
super(firstname, lastname);
//now setting the member variables of this class
this.employeeid = employeeid;
this.payscale = payscale;
this.joiningDate = joiningDate;
}
//show employee details
public void showDetail() {
System.out.println("Employee details:");
//calling the inherited method showName()
//of the parent class Person
//to show the first name and last name
this.showName();
//now calling the method of this class
this.showEmployeeDetail();
}
public void showEmployeeDetail() {
System.out.println("Employee ID: " + this.employeeid);
System.out.println("Pay scale: " + this.payscale);
System.out.println("Joining Date: " + this.joiningDate);
}
}
//the main class
public class Example {
//the main method
public static void main(String[] args) {
//employee data
String firstname = "";
String lastname = "";
String employeeid = "E01";
int payscale = 3;
String joiningDate = "2010-01-01";
//creating an object of the Employee class
Employee empObj = new Employee(firstname, lastname, employeeid, payscale, joiningDate);
//show detail
empObj.showDetail();
}
}
$javac Example.java $java Example Employee details: First name: Last name: Employee ID: E01 Pay scale: 3 Joining Date: 2010-01-01
访问隐藏在子类中的父类成员
super关键字的另一种用法是访问隐藏在子类中的父类的成员。
注意!如果子类成员变量与父类成员变量具有相同的名称,则父变量将隐藏在子类中。
例:
class Parent {
int x;
}
class Child extends Parent {
int x;
}
在上面的代码中,"孩子"类中的变量" x"隐藏了"父母"类中的变量" x"。
例2:访问父类成员变量
在下面的示例中,我们使用super关键字从子类访问父类的成员变量。
//the parent class
class Person {
//general member variables
String firstname;
String lastname;
//constructor
Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
//the child class inheriting the parent class
class Employee extends Person {
//member variable "firstname" of this class
//is hiding the member variable "firstname"
//of the parent class Person
//
//note! firstname is also present
//in the Person class and is inherited
//in this Employee class
String firstname = "Default firstname";
//specific member variables
String employeeid;
int payscale;
String joiningDate;
//constructor
Employee(String firstname, String lastname, String employeeid, int payscale, String joiningDate) {
//calling the constructor of the parent class Person
//to set the first name and last name
super(firstname, lastname);
//now setting the member variables of this class
this.employeeid = employeeid;
this.payscale = payscale;
this.joiningDate = joiningDate;
}
//show employee details
public void showDetail() {
System.out.println("Employee details:");
//this will give us the value stored in the
//firstname member variable of this class
System.out.println("First name (variable of Employee class): " + this.firstname);
//to get the firstname that was saved in the
//parent class member variable
//we have to use the super keyword
System.out.println("First name (variable of Person class): " + super.firstname);
//showing the last name
//this was inherited from the parent class Person
//but since it is not hidden in the child class
//Employee so, we are using the "this" keyword
//to access it
System.out.println("Last name (inherited variable of Employee class): " + this.lastname);
//we can also use the "super" keyword and
//access the lastname variable of the Person class
System.out.println("Last name (variable of Person class): " + super.lastname);
//now calling the method of this class
this.showEmployeeDetail();
}
public void showEmployeeDetail() {
System.out.println("Employee ID: " + this.employeeid);
System.out.println("Pay scale: " + this.payscale);
System.out.println("Joining Date: " + this.joiningDate);
}
}
//the main class
public class Example {
//the main method
public static void main(String[] args) {
//employee data
String firstname = "";
String lastname = "";
String employeeid = "E01";
int payscale = 3;
String joiningDate = "2010-01-01";
//creating an object of the Employee class
Employee empObj = new Employee(firstname, lastname, employeeid, payscale, joiningDate);
//show detail
empObj.showDetail();
}
}
$javac Example.java $java Example Employee details: First name (variable of Employee class): Default firstname First name (variable of Person class): Last name (inherited variable of Employee class): Last name (variable of Person class): Employee ID: E01 Pay scale: 3 Joining Date: 2010-01-01

