当前位置:   article > 正文

Java面向对象雇员类设计实验_person类中的属性有:姓名name(string类型),地址address(string类型)并

person类中的属性有:姓名name(string类型),地址address(string类型)并写出该类的

目录

一、实验目的

二、雇员类设计实验的内容及步骤

三、实验代码

Person类

Employee类:

Manager类

Add类 

Employee_Test类

 四、运行结果


一、实验目的

1、了解和掌握类的构造方法,继承方法的相关知识。

2、能够使用类的继承及其应用解决实际应用问题,如设计一个雇员类,并实现雇员对象基本信息的管理。

二、雇员类设计实验的内容及步骤

实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,

(1)Person类中的属性有:姓名(name,String),地址(address,String),定义该类的构造方法。

(2)Employee类中的属性有:工号(ID,String),工资(wage,double),工龄(workingyears,int),级别(level,String),定义该类的构造方法;

(3)Manager类中的属性有:部门(department,String),定义该类的构造方法;

(4)要求使用方法的重写(人,员工,经理类信息的输出方法);要求实现类的构造方法(super关键字

(5)编写一个测试类,产生一个员工和一个经理对象,输出员工和经理的基本信息;

(6)设计一个Add类用于涨工资,普通员工一次能涨10%,经理能涨20%。

(7)员工工龄超过3年的涨一次工资,部门经理工龄超过1年的涨一次工资。

三、实验代码

  • Person类

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package employee_test;
  7. /**
  8. *
  9. * @author caiqi
  10. */
  11. public class Person {
  12. private String stringName;
  13. private String stringAddress;
  14. public Person(String stringName, String stringAddress) {
  15. this.stringName = stringName;
  16. this.stringAddress = stringAddress;
  17. }
  18. public String getStringAddress() {
  19. return stringAddress;
  20. }
  21. public void setStringAddress(String stringAddress) {
  22. this.stringAddress = stringAddress;
  23. }
  24. public String getStringName() {
  25. return stringName;
  26. }
  27. public void setStringName(String stringName) {
  28. this.stringName = stringName;
  29. }
  30. @Override
  31. public String toString() {
  32. System.out.println(super.toString());
  33. return "Person{" + "姓名=" + stringName + ", 地址=" + stringAddress + "};";
  34. }
  35. }
  • Employee类:

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package employee_test;
  7. /**
  8. *
  9. * @author caiqi
  10. */
  11. public class Employee extends Person{
  12. private String ID;
  13. private double wage;
  14. private int workingyears;
  15. private String level;
  16. public Employee(String ID, double wage, int workingyears, String level, String stringName, String stringAddress) {
  17. super(stringName, stringAddress);
  18. this.ID = ID;
  19. this.wage = wage;
  20. this.workingyears = workingyears;
  21. this.level = level;
  22. }
  23. public String getLevel() {
  24. return level;
  25. }
  26. public void setLevel(String level) {
  27. this.level = level;
  28. }
  29. public int getWorkingyears() {
  30. return workingyears;
  31. }
  32. public void setWorkingyears(int workingyears) {
  33. this.workingyears = workingyears;
  34. }
  35. public double getWage() {
  36. return wage;
  37. }
  38. public void setWage(double wage) {
  39. this.wage = wage;
  40. }
  41. public String getID() {
  42. return ID;
  43. }
  44. public void setID(String ID) {
  45. this.ID = ID;
  46. }
  47. @Override
  48. public String toString() {
  49. System.out.println(super.toString());
  50. return "Employee{" + "工号=" + ID + ", 工资=" + wage + "元, 工龄=" + workingyears + "年, 级别=" + level + "}";
  51. }
  52. }
  • Manager类

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package employee_test;
  7. /**
  8. *
  9. * @author caiqi
  10. */
  11. public class Manager extends Employee{
  12. private String department;
  13. public Manager(String department, String ID, double wage, int workingyears, String level, String stringName, String stringAddress) {
  14. super(ID, wage, workingyears, level, stringName, stringAddress);
  15. this.department = department;
  16. }
  17. public String getDepartment() {
  18. return department;
  19. }
  20. public void setDepartment(String department) {
  21. this.department = department;
  22. }
  23. @Override
  24. public String toString() {
  25. System.out.println(super.toString());
  26. return "Manager{" + "部门=" + department + "}";
  27. }
  28. }
  • Add类 

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package employee_test;
  7. /**
  8. *
  9. * @author caiqi
  10. */
  11. public class Add {
  12. String AddWageJg="工资没变";
  13. public String AddWage(Employee M){
  14. if (M.getLevel().equals("普通员工")){
  15. if (M.getWorkingyears()>=3){
  16. M.setWage(M.getWage()*1.1);
  17. AddWageJg="工资涨了,现在的工资为"+M.getWage();
  18. }
  19. }
  20. if (M.getLevel().equals("经理")){
  21. if (M.getWorkingyears()>=1){
  22. M.setWage(M.getWage()*1.2);
  23. AddWageJg="工资涨了,现在的工资为"+M.getWage()+"元";
  24. }
  25. }
  26. return AddWageJg;
  27. }
  28. }
  • Employee_Test类

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package employee_test;
  7. /**
  8. *
  9. * @author caiqi
  10. */
  11. public class Employee_Test {
  12. public static void main(String[] args) {
  13. Employee EmployeeA=new Employee("22222", 3000.00, 2, "普通员工", "李四","中国上海");
  14. Manager ManagerA=new Manager("市场营销部","111",8000.00,5,"经理","张三","中国台湾");
  15. Add AddA=new Add();
  16. System.out.println(EmployeeA.toString());
  17. System.out.println("---------------------------------------------------------------------------------------");
  18. System.out.println(ManagerA.toString());
  19. System.out.println("---------------------------------------------------------------------------------------");
  20. System.out.println(EmployeeA.getLevel()+EmployeeA.getStringName()+":"+AddA.AddWage(EmployeeA));
  21. System.out.println(ManagerA.getLevel()+ManagerA.getStringName()+":"+AddA.AddWage(ManagerA));
  22. }
  23. }

 四、运行结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/136452
推荐阅读
相关标签
  

闽ICP备14008679号