java用最终成员变量和抽象方法,为一个Java州立大学的学生建立账单系统,州内外的学生收费不同,州内每学分收费¥750,州外为¥2000 ,每个学生的账单上有学校名称学生姓名信用卡使用的时间以及账单的总数
时间: 2023-12-03 10:45:12 浏览: 61
基于JAVA技术的学校收费管理系统的实现.docx
好的,为了实现这个账单系统,我们可以定义一个抽象类`Student`,其中包含最终的成员变量`SCHOOL_NAME`和抽象方法`calculateTotalBill()`。具体实现如下:
```java
public abstract class Student {
protected final String SCHOOL_NAME = "Java州立大学";
protected String studentName;
protected int credits;
public Student(String studentName, int credits) {
this.studentName = studentName;
this.credits = credits;
}
public abstract double calculateTotalBill();
}
```
然后,我们可以定义两个具体的子类`InStateStudent`和`OutStateStudent`,分别代表州内学生和州外学生。这两个子类应该实现`calculateTotalBill()`方法,根据不同的州内外状态计算账单总额。
```java
public class InStateStudent extends Student {
private final double IN_STATE_TUITION = 750.0;
public InStateStudent(String studentName, int credits) {
super(studentName, credits);
}
@Override
public double calculateTotalBill() {
return credits * IN_STATE_TUITION;
}
}
public class OutStateStudent extends Student {
private final double OUT_STATE_TUITION = 2000.0;
public OutStateStudent(String studentName, int credits) {
super(studentName, credits);
}
@Override
public double calculateTotalBill() {
return credits * OUT_STATE_TUITION;
}
}
```
最后,我们可以在主函数中创建学生对象并调用`calculateTotalBill()`方法计算账单总额,并输出账单信息。
```java
public static void main(String[] args) {
Student inStateStudent = new InStateStudent("Tom", 12);
Student outStateStudent = new OutStateStudent("Jerry", 18);
System.out.println(inStateStudent.SCHOOL_NAME);
System.out.println("Student Name: " + inStateStudent.studentName);
System.out.println("Total Bill: $" + inStateStudent.calculateTotalBill());
System.out.println(outStateStudent.SCHOOL_NAME);
System.out.println("Student Name: " + outStateStudent.studentName);
System.out.println("Total Bill: $" + outStateStudent.calculateTotalBill());
}
```
输出结果如下:
```
Java州立大学
Student Name: Tom
Total Bill: $9000.0
Java州立大学
Student Name: Jerry
Total Bill: $36000.0
```
阅读全文