赞
踩
1、实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一个方法add用于涨工资,普通员工一次能涨10%,经理涨20%,具体要求如下:
(1)Person类中的属性有:name、address地址定义该类的构造方法
(2)Employee类中的属性有:工号id、工资wage、工龄workAge,定义该类的构造方法
(3)Manager类中的属性有:级别level,定义该类的构造方法
(4)编写一个测试类,创建一个员工和一个经理对象,给该员工和经理涨工资,并输出其有的信息。
代码块部分
- package com.company.Demo3;
-
- public class Person {
- public String name;
- public String address;
-
- public Person(String name,String address){
- this.name=name;
- this.address=address;
-
- }
- }
- class Employee extends Person{
- public int id;
- public double wage;
- public int workAge;
-
- public Employee(String name,String address,int id,double wage,int workAge){
- super(name, address); //调用父类的构造方法
- this.id=id;
- this.wage=wage;
- this.workAge=workAge;
-
-
- }
- public double add(String position){//判断职位
- if (position =="0"){
- wage = wage *1.2;
- }else {
- wage = wage *1.1;
- }
- return wage;
-
- }
-
- public double getWage() {
- return wage;
- }
-
- public void setWage(double wage) {
- this.wage = wage;
- }
- }
- class Manager extends Employee{
- private String level = ""; //1为普通员工 0为经理
-
- public Manager(String name,String address,int id,double wage,int workAge,String level){
- super(name,address,id,wage,workAge);
- this.level=level;
-
- }
-
- public String getLevel() {
- return level;
- }
-
- public void setLevel(String level) {
- this.level = level;
- }
- }
- Test.java
- -----------
- package com.company.Demo3;
- public class Test {
- public static void main(String[] args) {
- Manager employees = new Manager("小华","上海",10,3500,5,"1");
- Manager manager = new Manager("小明","上海",20,5000,10,"0");
- //传入职位,Manager类型的对象没有add()方法,所有自动搜寻其父类,调用add方法
- employees.add(employees.getLevel());
- manager.add(manager.getLevel());
-
- System.out.println("e wage is :" + employees.getWage());
- System.out.println("m wage is :" + manager.getWage());
- }
-
- }
运行结果为
总结:
学会一个简单的继承,以及构造器的生成,super在构造器中的作用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。