Java第12次作业

1.

package
java11; public class ColaEmployee { private String name; private int 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 ColaEmployee(String name,int month){ this.month=month; this.name=name; } double getSalary(int m){ int a; if(month==m){ a=100; }else{ a=0; } return a; } } package java11; public class SalariedEmployee extends ColaEmployee{ private int monthsala; public SalariedEmployee(String name, int month,int monthsala) { super(name, month); this.monthsala=monthsala; } double getSalary(int m){ return monthsala+super.getSalary(m); } } package java11; public class HourlyEmployee extends ColaEmployee{ private int time; private double hoursal; public HourlyEmployee(String name, int month,int time,double hoursal) { super(name, month); this.hoursal=hoursal; this.time=time; } double getSalary(int m){ double sal; if(time<=160){ sal=time*hoursal; }else{ sal=160*hoursal+(time-160)*1.5*hoursal; } return sal+super.getSalary(m); } } package java11; public class SalesEmployee extends ColaEmployee{ private double ysh; private double tc; public SalesEmployee(String name, int month,double ysh,double tc) { super(name, month); this.tc=tc; this.ysh=ysh; } double getSalary(int m){ return tc*ysh+super.getSalary(m); } } package java11; import java.util.Scanner; public class Company { int m=new Scanner(System.in).nextInt(); void show(ColaEmployee em){ System.out.println(em.getName()+"的工资是"+em.getSalary(m)); } } package java11; public class test { public static void main(String[] args) { ColaEmployee []ColaEmployee=new ColaEmployee[3]; ColaEmployee[0]=new SalariedEmployee("固定员工",10,10000); ColaEmployee[1]=new HourlyEmployee("小时员工",3,170,13); ColaEmployee[2]=new SalesEmployee("销售",7,50000,0.23); Company company=new Company(); for(ColaEmployee i:ColaEmployee){ company.show(i); } } }

2.

package java11d2;

import java.util.Scanner;

public class Gardener {

    public static void main(String[] args) {
        Gardener g=new Gardener();
        g.creat();
    }
    public Fruit creat(){
        Fruit f=null;
        Scanner sc=new Scanner(System.in);
        String name=sc.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 java11d2;

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("创建了一个葡萄类的对象");
    }
}

posted @ 2021-06-14 19:05  jth12  阅读(40)  评论(0编辑  收藏  举报