第15次作业

1.

package lj;

public class ColaEmployee {
    protected String name;
          protected int month;
      
          public ColaEmployee() {
              super();
      
         }
     
         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 double getSalary(int month) {
             return 0;
     
        }
     
     }
package lj;

public class SalariedEmployee extends ColaEmployee {
    double monSalary;
      
          public SalariedEmployee() {
              super();
      
          }
     
         public SalariedEmployee(String name, int month, double monSalary) {
             super(name, month);
             this.monSalary = monSalary;
     
         }
     
         public double getSalary(int month) {
             if (super.getMonth() == month) {
                 return monSalary + 100;
             } else {
                 return monSalary;
            }
        }
     }
    
package lj;

public class HourlyEmployee extends ColaEmployee {
    int hourSalary;
          int hourNum;
      
          public HourlyEmployee() {
              super();
      
         }
     
         public HourlyEmployee(String name, int month, int hourSalary, int hourNum) {
             super(name, month);
             this.hourSalary = hourSalary;
             this.hourNum = hourNum;
         }
     
         public double getSalary(int month) {
             if (super.getMonth() > month) {
                 if (hourNum > 160) {
                     return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5
                             + 100;
                 } else {
                     return hourSalary * hourNum + 100;
     
                }
            } else {
                 if (hourNum > 160) {
                     return hourSalary * 160 + hourSalary * (hourNum - 160) * 1.5;
                 } else {
                    return hourSalary * hourNum;
    
                }
            }
        }
     
 }
package lj;

public class SalesEmployee extends ColaEmployee {
    int monthSales;
          double royaltyRate;
      
          public SalesEmployee() {
              super();
      
         }
     
         public SalesEmployee( String name,int month, int monthSales,
                 double royaltyRate) {
             super(name, month);
             this.monthSales = monthSales;
             this.royaltyRate = royaltyRate;
         }
     
         public double getSalary(int month) {
             if (super.getMonth() == month) {
                 return monthSales * royaltyRate + 100;
             } else {
                 return monthSales * royaltyRate;
             }
         }
     
    }
package lj;

public class Company {
    public void getSalary(ColaEmployee c, int month) {
         System.out.println(c.name + "在" + month + "月的月薪为" + c.getSalary(month)+ "元");
         }

}
package lj;

public class TestCompany {
    public static void main(String[] args) {
                  
                ColaEmployee[] cel = {
                         new SalariedEmployee("salariedEmployee", 6, 30000),
                         new HourlyEmployee("hourlyEmployee", 5, 100, 300),
                         new SalesEmployee("salesEmployee", 3, 500000, 0.3) 
                 };
                // 数组遍历
                for (int i = 0; i < cel.length; i++) {
                    new Company().getSalary(cel[i], 10);
                 }
         
            }


}

 

 2.

package lj;

import java.util.Scanner;

public interface Fruit {

    

}
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 Gardener {
     public Fruit create() {
         Fruit f = null;
         Scanner input = new Scanner(System.in);
        String name = input.next();
         if (name.equals("苹果")) {
             f = new Apple();
         } else if (name.equals("香蕉")) {
             f = new Banana();
         } else if (name.equals("葡萄")) {
             f = new Putao();
         } else {
            System.out.println("不会种");
         }
         return f;
 
     }
 }
package lj;

public class Test2 {

    /**
     * @param args
     */
     public static void main(String[] args) {
             Gardener g = new Gardener();
                 g.create();
         
      }
}

 

posted on 2021-06-14 21:21  觊·觎  阅读(45)  评论(0编辑  收藏  举报

导航