Java第十五周作业
package gkd; public abstract class ColaEmployee { private String name; private int month; public ColaEmployee(){ } public ColaEmployee(String name, int month) { super(); this.name = name; this.month = month; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public abstract double getSalary(int month); }
package gkd; public class Company { public static void print(ColaEmployee[] colaEmployees){ int month = 1; for (int i = 0; i < colaEmployees.length; i++) { System.out.println("工資是"+colaEmployees[i].getSalary(month)); } } }
package gkd; public class Company { public static void print(ColaEmployee[] colaEmployees){ int month = 1; for (int i = 0; i < colaEmployees.length; i++) { System.out.println("工資是"+colaEmployees[i].getSalary(month)); } } }
package gkd; public class SalariedEmployee extends ColaEmployee{ private double monthMoney; public SalariedEmployee() { super(); } public SalariedEmployee(String name, int month, double monthMoney) { super(name, month); this.monthMoney = monthMoney; } public double getMonthMoney() { return monthMoney; } public void setMonthMoney(double monthMoney) { this.monthMoney = monthMoney; } public double getSalary(int month){ if (super.getMonth() == month) { return this.monthMoney + 100; } return this.monthMoney; } }
package gkd; public class SalesEmployee extends ColaEmployee{ private double monthlySales; private double rate; public SalesEmployee() { super(); } public SalesEmployee(String name, int month, double monthlySales, double rate) { super(name, month); this.monthlySales = monthlySales; this.rate = rate; } public double getMonthlySales() { return monthlySales; } public void setMonthlySales(double monthlySales) { this.monthlySales = monthlySales; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } public double getSalary(int month){ if (super.getMonth() == month) { return this.monthlySales * this.rate + 100; } return this.monthlySales * this.rate; } }
package gkd; public class Test { public static void main(String[] args) { ColaEmployee hourlyEmployee = new HourlyEmployee("張三", 6, 45, 180); ColaEmployee salariedEmployee = new SalariedEmployee("李四", 3, 9000); ColaEmployee salesEmployee = new SalesEmployee("王五", 7, 500000, 0.03); ColaEmployee[] colaEmployees = new ColaEmployee[]{hourlyEmployee, salariedEmployee, salesEmployee}; Company.print(colaEmployees); } }
package gkd2; public class Apple implements Fruit{ public Apple() { System.out.println("创建了苹果的对象"); } }
package gkd2; public interface Fruit { }
package gkd2; import java.util.Scanner; public class Gardener { public void create(String fruit){ if("apple".equals(fruit)) { Fruit apple = new Apple(); }else if("oranges".equals(fruit)) { Fruit oranges = new Oranges(); }else if("pear".equals(fruit)) { Fruit pear = new Pear(); } } public static void main(String[] args) { Gardener gardener = new Gardener(); Scanner s = new Scanner(System.in); System.out.println("请输入想要创造的水果"); gardener.create(s.next()); } }
package gkd2; public class Oranges implements Fruit{ public Oranges() { System.out.println("创建橘子的对象"); } }
package gkd2; public class Pear implements Fruit{ public Pear() { System.out.println("创建梨的对象"); } }