super关键字与this关键字的使用 - 系统升级

话不多说,先看题

小明工作的公司最近要升级系统了,原先的员工管理系统有一个Employee类用于管理员工的姓名、出生年月、职位等个人信息,现在老板希望新增加一个Salary类用于管理员工的工资。小明公司的老板非常抠门,他要求Salary类要继承Employee类,以减小后期对系统的维护成本。小明感觉无从下手,便请你来帮忙。

在这里插入图片描述

初始的代码如下:

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 *********/

根据要求

类进行初始化,初始化的简便方法(适用于eclipse)

Source→Generate Constructor using Fields…→Generate
在这里插入图片描述
具体的构造方法中的内容在这我就不解释了,相信大家应该都知道

在Salary类使用super关键字调用父类的方法。
首先题目中测试类里面使用的是

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

明显是有四个参数的传递,而在Employee类中构造方法只有三个参数,明显不足以接收,这也就是需要我们使用Salary类了,我们对Employee进行继承,然后再增加一个salary参数,这样参数就正好四个,足以接收所有数据了

所以接下来就是在salary类中创建构造方法使用继承,将name,position,birth值录入,再单独将salary的值录入

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

当所需的值都录入好以后,就按要求创建introduction方法即可
相似我也是用到继承

最后的代码如下:

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("员工姓名:"+name+" 出生年月:"+birth+" 职位:"+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);
	}
}
/********** End *********/

我对本题的思路以及代码,都在这了,本文主要是记录自己写题时的一些思路,也方便以后对知识的回顾,仅供大家参考参考,如果文章内容有什么不对的地方,还请看见的大佬指正指正。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值