码迷,mamicode.com
首页 > 其他好文 > 详细

十五周

时间:2021-06-18 19:32:56      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ann   demo   class   调用   method   ace   苹果   this   create   

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 com.wahaha.Demo;
    public class Hepo {
        public static void main(String[] args) {
            Employee e1 = new SalariedEmployee("张三", 6, 9000);
            e1.getSalary(6);
            
            Employee e2 = new HourlyEmployee("李四", 7, 60, 200);
            e2.getSalary(6);
            
            Employee e3 = new HourlyEmployee("王五", 2, 60, 150);
            e3.getSalary(6);
            
            Employee e4 = new SalesEmployee("赵六", 11, 70000.0, 0.15);
            e4.getSalary(6);
            
            Employee e5 = new BasePlusSalesEmployee("孙子", 4, 10000, 0.10, 8000.0);
            e5.getSalary(6);
            
        
        }
        
    }


    abstract class Employee {
        private String name;
        private int birthdayMonth;

        public Employee() {
            super();
        }

        public Employee(String name, int birthdayMonth) {
            super();
            this.name = name;
            this.birthdayMonth = birthdayMonth;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getBirthdayMonth() {
            return birthdayMonth;
        }

        public void setBirthdayMonth(int birthdayMonth) {
            this.birthdayMonth = birthdayMonth;
        }
        
        public abstract void getSalary(int month);
    }


    class SalariedEmployee extends Employee {
        private double monthSalary;

        public SalariedEmployee() {
            super();
        }

        public SalariedEmployee(String name, int birthdayMonth, double monthSalary) {
            super(name, birthdayMonth);
            this.monthSalary = monthSalary;
        }

        public double getMonthSalary() {
            return monthSalary;
        }

        public void setMonthSalary(double monthSalary) {
            this.monthSalary = monthSalary;
        }


        public void getSalary(int month) {
        
            
            System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (monthSalary + 100) : monthSalary));
        }

    }


    class HourlyEmployee extends Employee {
        private double hourlySalary;
        private int hours;

        public HourlyEmployee() {
            super();
        }

        public HourlyEmployee(String name, int birthdayMonth, double hourlySalary, int hours) {
            super(name, birthdayMonth);
            this.hourlySalary = hourlySalary;
            this.hours = hours;
        }

        public double getHourlySalary() {
            return hourlySalary;
        }

        public void setHourlySalary(double hourlySalary) {
            this.hourlySalary = hourlySalary;
        }

        public int getHours() {
            return hours;
        }

        public void setHours(int hours) {
            this.hours = hours;
        }

        
        public void getSalary(int month) {
            
            double salary = 0;
            
            if(hours <= 160) {
                salary = hourlySalary * hours;
            } else {
                salary = ((hours - 160) * 1.5  + 160) * hourlySalary;
            }
            
            System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (salary + 100) : salary));
        }
    }


    class SalesEmployee extends Employee {
        private double saleroom;
        private double rate;

        public SalesEmployee() {
            super();
        }

        public SalesEmployee(String name, int birthdayMonth, double saleroom, double rate) {
            super(name, birthdayMonth);
            this.saleroom = saleroom;
            this.rate = rate;
        }

        public double getSaleroom() {
            return saleroom;
        }

        public void setSaleroom(double saleroom) {
            this.saleroom = saleroom;
        }

        public double getRate() {
            return rate;
        }

        public void setRate(double rate) {
            this.rate = rate;
        }

        public void getSalary(int month) {
            System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (saleroom * rate + 100) : saleroom * rate));
        }

    }


    class BasePlusSalesEmployee extends SalesEmployee {
        private double basicSalary;

        public BasePlusSalesEmployee() {
            super();
        }

        public BasePlusSalesEmployee(String name, int birthdayMonth, double saleroom, double rate, double basicSalary) {
            super(name, birthdayMonth, saleroom, rate);
            this.basicSalary = basicSalary;
        }

        public double getBasicSalary() {
            return basicSalary;
        }

        public void setBasicSalary(double basicSalary) {
            this.basicSalary = basicSalary;
        }
        public void getSalary(int month) {
            double salary = getSaleroom() * getRate() + basicSalary;
            
            System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (salary + 100) : salary));
        }
        
    }

 


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

 

public interface jkFruit {
}
class Apple implements jkFruit{
    public Apple(){
        System.out.println("创建了一个苹果类的对象");
    }
}
    class Banana implements jkFruit{
        public Banana(){
            System.out.println("创建了一个香蕉类的对象");
        }
    }
    
    class Grape implements jkFruit{
        public Grape(){
            System.out.println("创建了一个葡萄类的对象");
        }
    }


import java.applet.Applet;
import java.util.Scanner;
public class Gardener {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Gardener g=new Gardener();
        g.create();
    }
    public jkFruit create(){
        Scanner input=new Scanner(System.in);
        String name=input.next();
        jkFruit f=null;
        if(name.equals("苹果")){
            f=new Apple();
        }
        if(name.equals("香蕉")){
            f=new Banana();
        }
        if(name.equals("葡萄")){
            f=new Grape();
        }
        return f;
    }
}

 

十五周

标签:ann   demo   class   调用   method   ace   苹果   this   create   

原文地址:https://www.cnblogs.com/liualiu1/p/14897874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!