我只是在为即将到来的考试做一些练习,但有一件事我无法理解,那就是使用一个属于一个班级的变量,在另一个班级。
我有一个课程班和一个学生班。Class course 存储所有不同的课程,我只想在学生类中使用课程的名称。
这是我的课程:
public class Course extends Student
{
// instance variables - replace the example below with your own
private Award courseAward;
private String courseCode;
public String courseTitle;
private String courseLeader;
private int courseDuration;
private boolean courseSandwich;
/**
* Constructor for objects of class Course
*/
public Course(String code, String title, Award award, String leader, int duration, boolean sandwich)
{
courseCode = code;
courseTitle = title;
courseAward = award;
courseLeader = leader;
courseDuration = duration;
courseSandwich = sandwich;
}
}
这是学生:
public class Student
{
// instance variables - replace the example below with your own
private int studentNumber;
private String studentName;
private int studentPhone;
private String studentCourse;
/**
* Constructor for objects of class Student
*/
public Student(int number, String name, int phone)
{
studentNumber = number;
studentName = name;
studentPhone = phone;
studentCourse = courseTitle;
}
}
我在课程中使用“扩展”是否正确?或者这是不必要的?
在我的学生构造函数中,我试图将课程课程中的“课程标题”分配给变量“学生课程”。但我根本无法弄清楚如何做到这一点!
提前感谢您的帮助,我期待您的回音!
谢谢!