java十五周作业

Cola公司的雇员分为以下若干类:(知识点:多态) [必做
]
• 4.1 ColaEmployee :这是所有员工总的父类,属性:员工的
姓名,员工的生日月份。方法:getSalary(int month) 根据参数
月份来确定工资,如果该月员工过生日,则公司会额外奖励
100 元。
• 4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工
资的员工。属性:月薪
课后作业
• 4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工
资的员工,每月工作超出160 小时的部分按照1.5 倍工资发
放。属性:每小时的工资、每月工作的小时数
• 4.4 SalesEmployee :ColaEmployee 的子类,销售人员,
工资由月销售额和提成率决定。属性:月销售额、提成率
• 4.5 定义一个类Company,在该类中写一个方法,调用该
方法可以打印出某月某个员工的工资数额,写一个测试类
TestCompany,在main方法,把若干各种类型的员工放在一
ColaEmployee 数组里,并单元出数组中每个员工当月的
工资。

 

package Class;

public class class1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColaEmployee TestCompany[] = new ColaEmployee[4];
        TestCompany[0] = new SalariedEmployee("赵凯",6,6,3000);
        TestCompany[1] = new HourlyEmployee("高粱",6,6,600,170);
        TestCompany[2] = new HourlyEmployee("赵晨辉",7,4,800,140);
        TestCompany[3] = new SalesEmployee("刘德璞",6,6,16000,0.43);
    }

}
class  ColaEmployee{
    String name;
    int birthdaymonth;
    int mongth;
    
    public ColaEmployee(String name, int birthdaymonth, int mongth) {
        super();
        this.name = name;
        this.birthdaymonth = birthdaymonth;
        this.mongth = mongth;
    }

    void getSalary(int month) {
        
        }
    }

class SalariedEmployee extends ColaEmployee{
   int sale ;

public SalariedEmployee(String name, int birthdaymonth, int mongth, int sale) {
    super(name, birthdaymonth, mongth);
    this.sale = sale;
    if (super.birthdaymonth==super.mongth) {
        sale = sale+100;
        System.out.println(name+"员工的工资为:"+sale);
    } else {
       System.out.println(name+"员工的工资为:"+sale);
    }
}

}
class HourlyEmployee extends ColaEmployee{
   double sale ;
   int hoursale;
   int hour;
public HourlyEmployee(String name, int birthdaymonth, int mongth, double hoursale, int hour) {
    super(name, birthdaymonth, mongth);
    
    this.hoursale = (int) hoursale;
    this.hour = hour;
    if (super.birthdaymonth==super.mongth&&hour<160) {
        sale = hoursale*hour+100;
        System.out.println(name+"员工的工资为:"+sale);
    }
    if (super.birthdaymonth==super.mongth&&hour>160) {
        sale = hoursale*160+1.5*(hour-160)+100;
        System.out.println(name+"员工的工资为:"+sale);
    } 
    if (super.birthdaymonth!=super.mongth&&hour<160) {
        sale = hoursale*hour;
        System.out.println(name+"员工的工资为:"+sale);
    }
    if (super.birthdaymonth!=super.mongth&&hour>160) {
        sale = hoursale*160+1.5*(hour-160)+100;
        System.out.println(name+"员工的工资为:"+sale);
    } 
}
 
}
class  SalesEmployee extends ColaEmployee{ 
    int monthmonye;
    double tichenglv;
    int sale;
    public SalesEmployee(String name, int birthdaymonth, int mongth, int monthmonye, double tichenglv) {
        super(name, birthdaymonth, mongth);
        this.monthmonye = monthmonye;
        this.tichenglv = tichenglv;
        
        if (super.birthdaymonth==super.mongth) {
            sale = (int) (monthmonye*tichenglv+100);
            System.out.println(name+"员工的工资为:"+sale);
        } else {
            sale = (int) (monthmonye*tichenglv);
           System.out.println(name+"员工的工资为:"+sale);
        }
    }
    
    }

 

• 5、利用接口实现动态的创建对象[选做题]
• 5.1 创建4个类:
• 苹果
• 香蕉
• 葡萄
• 园丁
• 5.2 在三种水果的构造方法中打印一句话.
• 以苹果类为例
• class apple
• {
• public apple()
• {
• System.out.println(―创建了一个苹果类的对象‖);
}
• }
课后作业
• 类图如下:
• 5.3 要求从控制台输入一个字符串,根据字符串的
值来判断创建三种水果中哪个类的对象

 

package Class;

import java.util.Scanner;

public class class1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        garderner g = new garderner();
        g.creat();
    }

}
class apple implements fruit{
 public apple()
 {
 System.out.println("种了一个苹果");
}
 }
class banana implements fruit{
     public banana()
     {
     System.out.println("种了一个香蕉");
    }
     }
class putao implements fruit{
     public putao()
     {
     System.out.println("种了一个葡萄");
    }
     }
class garderner{
    public fruit creat() {
        fruit f = null;
        Scanner input = new Scanner(System.in);
        String name = input.next();
        if(name.equals("苹果")) {
            f = (fruit) new apple();
        }
        if(name.equals("香蕉")) {
            f = (fruit) new banana();
        }
        if(name.equals("葡萄")) {
            f = (fruit) new putao();
        }
        return f;
    }
    
}
interface fruit{
    
}

 

 

posted @ 2021-06-17 21:22  TS花间酒  阅读(52)  评论(0编辑  收藏  举报