用java语言编写程序
没什么好说的,都不需要注释,看代码:
(1)
import java.util.ArrayList;
import java.util.List;
public class TestTeacher {
private static List<Teacher> teachers = new ArrayList<Teacher>();
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 + ",我可以充当食物被吃掉!");
}
}
第一题很多不太理解,我们才学习最基础的,不太懂
其实题目的要求就是在告诉你怎么做了。当然,我多做了一些操作以达到最大化的代码复用。比如这里:
public Teacher(String name, String jobTitle, int lessons) {
this.name = name;
this.jobTitle = jobTitle;
this.lessons = lessons;
}
Teacher类我多加了个构造函数,然后子类声明更简单的构造函数,再通过super调用父构造函数,这样就省去了很多属性的初始化过程。就像这里:
public Professor(String name, int lessons) {
super(name, "教授", lessons);
}
而且这样一来保证“职称”这个字段是不变的,你在其他地方不必设置“职称”这个属性,以免误传。然后calcSalary方法就是题目要求要有的抽象方法,我又写了一个display方法,这个方法直接调用子类的calcSalary方法,这样子类就不用再去分别实现工资的显示了,而且子类也拥有这个方法。但是,因为calcSalary是抽象方法,所以每个子类都必须重写,分别实现各自不同的工资计算方法,比如AssociateProfessor(副教授)的计算方法就是固定工资3000+补贴30*补贴课时:
public double calcSalary() {
return 3000 + 30 * lessons;
}
然后这里teachers.add(new Professor("赵一", 8));
这一句当中,teachers变量是一个Teacher类型的容器,但是往容器里添加的却是Professor类,Proffesor是Teacher的子类,这就是多态。
这些都是非常基础的东西,我觉得上面的代码中有疑点的差不多就这些了。其实第一题的代码也不多,让eclipse自动对齐的话可能也就80行左右,这已经是非常少了。
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;
}
}
没有主类和输出