15
1.
package zxcv;
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;
}
}
class SalariedEmployee extends ColaEmployee {
double monthSalary;
public SalariedEmployee() {
super();
}
public SalariedEmployee(String name, int month, double monthSalary) {
super(name, month);
this.monthSalary = monthSalary;
}
public double getSalary(int month) {
if (super.getMonth() == month) {
return monthSalary + 100;
} else {
return monthSalary;
}
}
}
class HourlyEmployee extends ColaEmployee {
int hourSalary;
int hourNumber;
public HourlyEmployee() {
super();
}
public HourlyEmployee(String name, int month, int hourSalary, int hourNumber) {
super(name, month);
this.hourSalary = hourSalary;
this.hourNumber = hourNumber;
}
public double getSalary(int month) {
if (super.getMonth() > month) {
if (hourNumber > 160) {
return hourSalary * 160 + hourSalary * (hourNumber - 160) * 1.5 + 100;
} else {
return hourSalary * hourNumber + 100;
}
} else {
if (hourNumber > 160) {
return hourSalary * 160 + hourSalary * (hourNumber - 160) * 1.5;
} else {
return hourSalary * hourNumber;
}
}
}
}
class SalesEmployee extends ColaEmployee {
int monthSales;
double ticheng;
public SalesEmployee() {
super();
}
public SalesEmployee(String name, int month, int monthSales, double ticheng) {
super(name, month);
this.monthSales = monthSales;
this.ticheng = ticheng;
}
public double getSalary(int month) {
if (super.getMonth() == month) {
return monthSales * ticheng + 100;
} else {
return monthSales * ticheng;
}
}
}
class Company {
public void getSalary(ColaEmployee c, int month) {
System.out.println(c.name + "在" + month + "月的月薪为" + c.getSalary(month) + "元");
}
}
package chap11;
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub
ColaEmployee[] c = {
new SalariedEmployee("月薪工", 6, 4500),
new HourlyEmployee("小时工", 5, 10, 300),
new SalesEmployee("提成工", 3, 20000, 0.3)
};
for (int i = 0; i < c.length; i++) {
new Company().getSalary(c[i], 10);
}
}
}
2.
package zxcv;
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 Method{
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 ;
}
}