我在java中创建了三个类:
CollegeCourse
package CollegeCourse;
import javax.swing.JOptionPane;
public class CollegeCourse {
protected String Dept;
protected int CourseNum;
protected int Cred;
protected int fee;
protected int Charge = 120;
protected int labfee;
public void inputDepartment(){
Dept = JOptionPane.showInputDialog(null, "Enter Department");
}
public void inputCourseNumber(){
String CourseNumString = new String(" ");
CourseNumString = JOptionPane.showInputDialog(null, "Enter Course Number");
CourseNum = Integer.parseInt(CourseNumString);
}
public void inputCredits(){
String CredString = new String(" ");
CredString = JOptionPane.showInputDialog(null, "Enter Credits");
Cred = Integer.parseInt(CredString);
}
public void displayNonLabCourse(){
fee = Cred * Charge;
JOptionPane.showMessageDialog(null, Dept + CourseNum +
"\nNon-lab Course" + "\n" + Cred +" Credits" + "\nTotal fee is $" + fee);
}
}
LabCourse
package CollegeCourse;
import javax.swing.JOptionPane;
public class LabCourse extends CollegeCourse {
public void displayLabCourse(){
labfee = fee + 50;
JOptionPane.showMessageDialog(null, Dept + CourseNum +
"\nLab Course" + "\n" + Cred +" Credits" + "\nTotal fee is $" + fee);
}
}
UseCourse
package CollegeCourse;
public class UseCourse{
public static void main(String[] args) {
CollegeCourse college = new CollegeCourse();
college.inputDepartment();
college.inputCourseNumber();
college.inputCredits();
college.displayNonLabCourse();
}
}
当用户进入某个部门如“BIO”时,如何让UseCourse类显示LabCourse显示方法?
创建一个名为CollegeCourse的类,其中包含保存部门的数据字段 (例如,ENG),课程编号(例如,101),学分(例如3), 和课程费用(例如,360美元)。所有字段都是必需的参数 到建设者,费用除外,每小时120美元计算。 包括显示课程数据的display()方法。创建一个名为LabCourse的子类 增加50美元的课程费用。覆盖父类display()方法以指示 课程是一个显示所有数据的实验课程。写一个名为UseCourse的应用程序 提示用户提供信息。如果用户在以下任何部门中输入课程, 创建LabCourse:BIO,CHM,CIS或PHY。如果用户进入任何其他部门,请创建 不包括实验室费用的CollegeCourse。然后显示课程数据。保存文件 作为CollegeCourse.java,LabCourse.java和UseCourse.java。
答案 0 :(得分:2)
这与您的原始实现完全不同。但我相信它能满足您的需求。可能有更好的方法来实现它,但我只是根据你给我的信息来实现。希望你能理解这一切...
UseCourse:
import javax.swing.JOptionPane;
public class UseCourse
{
public UseCourse()
{
CollegeCourse college;
// Collect all the necessary data first to determine whether it is a lab course or not.
String dept = JOptionPane.showInputDialog(null, "Enter Department");
String courseNumString = JOptionPane.showInputDialog(null, "Enter Course Number");
int courseNum = Integer.parseInt(courseNumString);
String credString = JOptionPane.showInputDialog(null, "Enter Credits");
int cred = Integer.parseInt(credString);
// Now that we have the info we can ask the question.
if (dept.equals("BIO") | dept.equals("CHM") | dept.equals("CIS") | dept.equals("PHY"))
{college = new LabCourse(dept, courseNum, cred);}
else {college = new LabCourse(dept, courseNum, cred);}
college.display();
}
public static void main(String[] args)
{new UseCourse();}
}
CollegeCourse:
import javax.swing.JOptionPane;
public class CollegeCourse {
protected String dept;
protected int courseNum;
protected int cred;
protected int fee;
protected int Charge = 120;
protected int labfee;
public CollegeCourse(String dept, int courseNum, int cred)
{
this.dept = dept;
this.courseNum = courseNum;
this.cred = cred;
fee = cred * Charge;
}
public void display(){
JOptionPane.showMessageDialog(null, dept + courseNum +
"\nNon-lab Course" + "\n" + cred +" Credits" + "\nTotal fee is $" + fee);
}
}
LabCourse:
import javax.swing.JOptionPane;
public class LabCourse extends CollegeCourse
{
public LabCourse(String dept, int courseNum, int cred)
{super(dept, courseNum, cred);}
public void display()
{
fee += 50;
JOptionPane.showMessageDialog(null, dept + courseNum +
"\nLab Course" + "\n" + cred +" Credits" + "\nTotal fee is $" + fee);
}
}
答案 1 :(得分:1)
如果你想使用继承来做这个,那么你应该使用相同的方法名称,例如" displayCourse"在CollegeCourse和LabCourse课程。然后根据用户的输入:
CollegeCourse c = null;
if (userSelectedCollegeCourse) {
c = new CollegeCourse(param1, ...);
}else{
c = new LabCourse(param1, ...);
}
c.displayCourse();