用java语言编写程序

2024-11-22 00:44:34
推荐回答(2个)
回答1:

没什么好说的,都不需要注释,看代码:

(1)

import java.util.ArrayList;
import java.util.List;

public class TestTeacher {
private static List teachers = new ArrayList();
public static void main(String[] args) {
teachers.add(new Professor("赵一", 8));
teachers.add(new AssociateProfessor("钱二", 16));
teachers.add(new Instructor("孙三", 32));
teachers.add(new TeachingAssisant("李四", 64));
System.out.println("姓名\t职称\t税前工资");
for(Teacher teacher : teachers) {
teacher.display();
}
}

}
abstract class Teacher {
protected String name;
protected String jobTitle;
protected int lessons;
public Teacher(String name, String jobTitle, int lessons) {
this.name = name;
this.jobTitle = jobTitle;
this.lessons = lessons;
}
public abstract double calcSalary();

public void display() {
System.out.println(String.format("%s\t%s\t%.0f", name, jobTitle, calcSalary()));
}
}

class Professor extends Teacher {
public Professor(String name, int lessons) {
super(name, "教授", lessons);
}
@Override
public double calcSalary() {
return 5000 + 50 * lessons;
}
}
class AssociateProfessor extends Teacher {
public AssociateProfessor(String name, int lessons) {
super(name, "副教授", lessons);
}
@Override
public double calcSalary() {
return 3000 + 30 * lessons;
}
}
class Instructor extends Teacher {
public Instructor(String name, int lessons) {
super(name, "讲师", lessons);
}
@Override
public double calcSalary() {
return 2000 + 20 * lessons;
}
}
class TeachingAssisant extends Teacher {
public TeachingAssisant(String name, int lessons) {
super(name, "助教", lessons);
}
@Override
public double calcSalary() {
return 1000 + 10 * lessons;
}
}

(2)

public class TestFood {

public static void main(String[] args) {
Food apple = new Apple();
Food beaf = new Beaf();
apple.doEat();
beaf.doEat();
}

}
interface Food {
void doEat();
}

class Fruit {
protected static final String PROPERTY = "水果";
}
class Meat {
protected static final String PROPERTY = "肉类";
}

class Apple extends Fruit implements Food {
@Override
public void doEat() {
System.out.println("我是苹果,我属于" + PROPERTY + ",我可以充当食物被吃掉!");
}
}
class Beaf extends Meat implements Food {
@Override
public void doEat() {
System.out.println("我是牛肉,我属于" + PROPERTY + ",我可以充当食物被吃掉!");
}
}

回答2:

package entity;

public abstract class Teacher {
public double basePay;// 固定工资
public double subsidy;// 授课补贴单价
public String jopName;// 职称
public int workTimes;// 授课数量
public int id;// 人员编号

public String name;// 人员名称

/**
* 工资计算
*
* @return
*/
public abstract double countPay();
}
package entity;

public class Professor extends Teacher {

public Professor() {
this.jopName="教授";
this.basePay=500;
this.subsidy=50;
}

@Override
public double countPay() {
return this.basePay+this.subsidy*this.workTimes;
}

}

package entity;

public class Lecturer extends Teacher {

public Lecturer() {
this.jopName="讲师";
this.basePay=200;
this.subsidy=20;
}

@Override
public double countPay() {
return this.basePay+this.subsidy*this.workTimes;
}

}

package entity;

public class HelperTeacher extends Teacher {

public HelperTeacher() {
this.jopName="助教";
this.basePay=100;
this.subsidy=10;
}

@Override
public double countPay() {

return this.basePay+this.subsidy*this.workTimes;
}

}

package entity;

public class AssociateProfessor extends Teacher {

public AssociateProfessor() {
this.jopName="副教授";
this.basePay=300;
this.subsidy=30;
}

@Override
public double countPay() {
return this.basePay+this.subsidy*this.workTimes;
}

}

package entity;

public interface Food {
public String show();
}

package entity;

public class Meat {
public String name;
public String type="肉类";
public String doing="我可以充当食物被吃掉!";
}

package entity;

public class Beef extends Meat implements Food {

public Beef() {
this.name="牛肉";
}

@Override
public String show() {
String str="我是+"+name+",我属于"+type+","+doing;
System.out.println(str);
return str;
}

}

package entity;

public class Fruit {
public String name;
public String type="水果";
public String doing="我可以充当食物被吃掉!";
}

package entity;

public class Apple extends Fruit implements Food {

public Apple() {
this.name="苹果";
}

@Override
public String show() {
String str="我是+"+name+",我属于"+type+","+doing;
System.out.println(str);
return str;
}

}