Use of super keyword and this keyword - system upgrade

Keywords: Java Eclipse

If you don't say much, look at the questions first

Xiaoming's company is going to upgrade its system recently. The original employee management system has an employee class to manage the employee's name, date of birth, position and other personal information. Now the boss hopes to add a Salary class to manage the employee's Salary. The boss of Xiaoming company is very stingy. He requires the Salary class to inherit the Employee class to reduce the later maintenance cost of the system. Xiao Ming feels unable to start, so he asks you to help.

The initial code is as follows:

package step2;

import java.util.Scanner;

public class SystemUpdate {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        String birth = scanner.next();
        String position = scanner.next();
        double salary = scanner.nextDouble();
        Salary employee = new Salary(name, birth, position, salary);
        employee.introduction();
    }
}

/********** Begin *********/
class Employee {
    private String name;
    private String birth;
    private String position;

}

class Salary extends Employee {
    private double salary;
}
/********** End *********/

As required

① Class, a simple method of initialization (applicable to eclipse)

Source→Generate Constructor using Fields...→Generate

I won't explain the contents of the specific construction method here. I believe everyone should know

② Use the super keyword in the Salary class to call the methods of the parent class.
First of all, the test class in the topic uses

Salary employee = new Salary(name, birth, position, salary);

Obviously, there are four parameters to be passed, but the construction method in the Employee class has only three parameters, which is obviously not enough to receive. That is, we need to use the salary class. We inherit the employee and then add a salary parameter, so that there are exactly four parameters, which is enough to receive all data

So the next step is to create a constructor in the salary class, use inheritance, enter the values of name, position and birth, and then enter the values of salary separately

public Salary(String name, String birth, String position,double salary) {
		super(name,birth,position);
		this.salary = salary;
	}

③ After all the required values are entered, you can create the introduction method as required
Similar to ②, I also use inheritance

The final code is as follows:

package step2;

import java.util.Scanner;

public class SystemUpdate {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        String birth = scanner.next();
        String position = scanner.next();
        double salary = scanner.nextDouble();
        Salary employee = new Salary(name, birth, position, salary);
        employee.introduction();
    }
}

/********** Begin *********/
class Employee {
    private String name;
    private String birth;
    private String position;
    public Employee(String name, String birth, String position) {
		super();
		this.name = name;
		this.birth = birth;
		this.position = position;
	}

    public Employee() {
		super();
	}

    public void introduction() {
		System.out.print("Employee name:"+name+" date of birth:"+birth+" Position:"+position);
	}
}

class Salary extends Employee {
    private double salary;

    public Salary(String name, String birth, String position,double salary) {
		super(name,birth,position);
		this.salary = salary;
	}
	public void introduction() {
		super.introduction();
		System.out.print(" Salary:"+salary);
	}
}
/********** End *********/

My ideas and codes for this topic are here. This article mainly records some ideas when I write the topic, which is also convenient for the review of knowledge in the future. It is for your reference only. If there is anything wrong with the content of the article, please see the boss for correction.

Posted by jibster on Mon, 27 Sep 2021 04:15:38 -0700