1、编写程序完成不同种类的雇员的工资计算,要求:
(1)每个员工都有基本工资和绩效;
(2)不同种类雇员有不同的基本工资和绩效,通过继承多态的方式来实现;
(3)经理级别的员工还有业绩奖金,通过接口来设计;
(4)恰当设计类、成员、子类、接口之间的关系,正确计算工资。
//定义主类
public class Main {
public static void main(String[] args) {
Member b1 = new FirstMember();
System.out.println("//一等员工工资");
b1.display();
b1 = new SecondMember();
System.out.println("//二等员工工资");
b1.display();
b1 = new Manger();
System.out.println("//经理工资");
b1.display();
}
}
//奖金接口
public interface Bonus {
public abstract double show();
}
//一级员工类
public class FirstMember extends Member{ //定义一级员工继承Member类
public FirstMember() {
super(10000,5000);
}
public void display() {
System.out.println("您的奖金为"+(wage+raward)+"元");
}
}
//二级员工类
public class SecondMember extends Member{ //定义二级员工继承Member类
public SecondMember() {
super(8000,3500);
}
public void display() {
System.out.println("您的奖金为"+(wage+raward)+"元");
}
}
//经理类
public class Manger extends Member implements Bonus{ //定义经理类继承Member类实现奖金接口
public Manger() {
super(15000,5000);
}
public double show() {
return 1500;
}
public void display() {
System.out.println("您的奖金为"+(wage+raward+show())+"元");
}
}
//员工抽象类
public abstract class Member {
double wage;
double raward;
public Member() {
wage = 6000;
raward = 1500;
}
public Member(double a,double b) {
wage = a;
raward = b;
}
public abstract void display();
}
//测试类
public class Main {
public static void main(String[] args) {
Member b1 = new FirstMember();
System.out.println("//一等员工工资");
b1.display();
b1 = new SecondMember();
System.out.println("//二等员工工资");
b1.display();
b1 = new Manger();
System.out.println("//经理工资");
b1.display();
}
}
———————————————————————————————————————————
2、模拟电脑组装,不同的电脑组装需要实现CPU接口,硬盘接口和内存接口。恰当设计类与接口,以及其关系,实现电脑的组装,输出正确的结果。
//测试类
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("想要一台什么配置的电脑?自己来请按1,听我的请按2");
int t = s.nextInt();
if(t == 2) {
Computer c1 = new Computer();
c1.display();
}
else {
if(t == 1) {
System.out.println("请依次输入您需要的详细配置(CPU、硬盘、内存):");
String t1,t2,t3;
t1 = s.next();
t2 = s.next();
t3 = s.next();
Computer c2 = new Computer(t1,t2,t3);
c2.display();
}
}
}
}
//Computer类
public class Computer implements CPU,RAM,ROM{
String s1;
String s2;
String s3;
public Computer() {//无参构造(听我的)
s1 = "AMD R7-6600H";
s2 = "512G固态硬盘";
s3 = "16G内存条";
}
public Computer(String s1,String s2,String s3) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
public String display3() {
return s3;
}
public String display2() {
return s2;
}
public String display1() {
return s1;
}
public void display() {
System.out.println("组装好了!");
System.out.println("CPU:"+display1());
System.out.println("硬盘:"+display2());
System.out.println("内存:"+display3());
}
}
//CPU接口
public interface CPU {//CPU接口
public abstract String display1();
}
//RAM接口
public interface RAM {//内存接口
public abstract String display2();
}
//ROM接口
public interface ROM {//硬盘接口
public abstract String display3();
}