7

我只是在为即将到来的考试做一些练习,但有一件事我无法理解,那就是使用一个属于一个班级的变量,在另一个班级。

我有一个课程班和一个学生班。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;
    }

}

我在课程中使用“扩展”是否正确?或者这是不必要的?

在我的学生构造函数中,我试图将课程课程中的“课程标题”分配给变量“学生课程”。但我根本无法弄清楚如何做到这一点!

提前感谢您的帮助,我期待您的回音!

谢谢!

4

12 回答 12

12

我在课程中使用“扩展”是否正确?或者这是不必要的?

不幸的是,如果您想知道您的继承是否正确,请将extends替换为is-a。课程是学生?答案是不。这意味着你Course不应该扩展Student

一个学生可以参加一个Course,因此Student该类可以有一个类型的成员变量Course。如果您的模型指定(一个学生可以参加几门课程),您可以定义一个课程列表。

这是一个示例代码:

public class Student{
    //....
    private Course course;
    //...
    public void attendCourse(Course course){
       this.course = course;
    }
    public Course getCourse(){
       return course;
    }
}

现在,您可以拥有以下内容:

Student bob = new Student(...);
Course course = new Course(...);
bob.attendCourse(course);
于 2012-06-28T19:36:00.287 回答
4

我假设 Course不是Student,因此这些类之间的继承可能是一个坏主意。

于 2012-06-28T19:34:35.200 回答
4

您必须将它们公开。

更好的方法是将它们保持私有,并为该变量编写一个公共 getter。例如:

public Award getCourseAward(){
         return this.courseAward;
}
于 2012-06-28T19:34:48.627 回答
2

Course不应延长Student。如果要访问 的courseTitle字段Course,则需要将Course对象的引用传递给Student,然后执行 course.CourseTitle。

于 2012-06-28T19:35:26.720 回答
2

您不能从另一个类访问一个类的私有属性,这是 OOP 的主要原则之一:封装。您必须提供对这些属性的访问方法,您希望在类之外发布。常见的方法是 setter/getters - 如果你想让你的类不可变,那么只有 getters。看这里:http ://en.wikipedia.org/wiki/Mutator_method#Java_example

于 2012-06-28T19:35:45.593 回答
2

任意扩展类是没有意义的。学生不是课程,反之亦然,因此您不能像那样扩展它们。

你需要做的是:

首先创建一个课程:

       Course aCourse = new Course(..);

创建学生:

       Student aStudent = new Student(..);

将课程分配给学生:

       aStudent.setCourse(aCourse.title);
于 2012-06-28T19:38:45.550 回答
2

扩展 StudentCouse因为它们不是同一类。当专攻一个更通用的(在某种意义上)一个类时,就会用另一个类扩展一个类。解决方案是作为构造函数的参数
传递courseTitleStudent

于 2012-06-28T19:39:40.610 回答
2

这里应该有 3 个单独的对象,一个课程、一个学生和一个注册。一个注册将一个学生连接到一个课程,一个课程有很多学生,一个学生可以注册很多课程。它们都不应该相互扩展。

于 2012-06-28T19:39:56.133 回答
2

也许您不需要将课程名称添加到学生。我要做的是将学生添加到课程中的一些数据结构中。这样更简洁,减少了 Course 和 Student 之间的耦合。这也将允许您让学生参加不止一门课程。例如:

public class Course extends Student{
    private Award courseAward;
    private String courseCode;
    public String courseTitle;
    private Student courseLeader;//change to a student Object
    private int courseDuration;
    private boolean courseSandwich;
    private Set<Student> students;//have course hold a collection of students

/**
 * Constructor for objects of class Course
 */
public Course(String code, String title, Award award, Student leader, int duration, boolean sandwich){
    courseCode = code;
    courseTitle = title;
    courseAward = award;
    courseLeader = leader;
    courseDuration = duration;
    courseSandwich = sandwich;
    this.students=new HashSet<Student>();
}

public boolean addStudent(Student student){
    return students.add(student);
}

public Set<Student> getStudents(){
    return students;
} 

}

于 2012-06-28T19:40:43.153 回答
2

第一的,

您在 Course 类中扩展 Student 类,这意味着,学生类获取所有 coruse 类属性。因此,学生类没有 courseTitle 属性。

其次,是的,这是不必要的 - 您需要执行以下操作:

public class Course
{
    private Award courseAward;
    private String courseCode;
    public String courseTitle;
    private String courseLeader;
    private int courseDuration;
    private boolean courseSandwich;

    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 
{
    private int studentNumber;
    private String studentName;
    private int studentPhone;

    // This is where you keep the course object associated to student
    public Course studentCourse;

    public Student(int number, String name, int phone, Course course)
    {
        studentNumber = number;
        studentName = name;
        studentPhone = phone;
        studentCourse = course;
    }  
}

示例用法如下所示:

Course course = new Course("ASD", "TITLE", null, "ME", 50, true);   
Student student = new Student(1, "JOHN", "5551234", course);

然后,通过以下方式从学生那里获取您需要的课程信息:

student.studentCourse.courseTitle;

从现在起 student.studentCourse 将是一个具有所有属性的课程对象。

干杯,

于 2012-06-28T19:41:03.847 回答
1

如前所述,请远离“扩展”。一般来说,除非“is-a”关系有意义,否则不应使用它。

您可能应该为 Course 类的方法提供 getter:

public class Course {
   ...
   public String getTitle() {
       return title;
   }
}

然后,如果 Student 类需要它,它会以某种方式获取课程(这取决于您的设计),并调用 getter:

public class Student {
   private Set<Course> courses = new HashSet<Course>();

   public void attendCourse(Course course) {
       courses.add(course);
   }

   public void printCourses(PrintStream stream) {
       for (Course course : courses) {
           stream.println(course.getTitle());
       }
   }
}
于 2012-06-28T22:05:55.400 回答
1

在下面找到您的问题的解决方案,如果您想在您的机器上检查以下代码,请创建一个名为 Test.java 的文件并粘贴以下代码:

包com;

class Course
{
    private Award courseAward;
    private String courseCode;
    public String courseTitle;
    private String courseLeader;
    private int courseDuration;
    private boolean courseSandwich;


    public Course(String code, String title, Award award, String leader, int duration, boolean sandwich)
    {
        courseAward = award;
        courseCode = code;
        courseTitle = title;
        courseLeader = leader;
        courseDuration = duration;
        courseSandwich = sandwich;

    }

    public Award getCourseAward() {
        return courseAward;
    }

    public void setCourseAward(Award courseAward) {
        this.courseAward = courseAward;
    }

    public String getCourseCode() {
        return courseCode;
    }

    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public String getCourseTitle() {
        return courseTitle;
    }

    public void setCourseTitle(String courseTitle) {
        this.courseTitle = courseTitle;
    }

    public String getCourseLeader() {
        return courseLeader;
    }

    public void setCourseLeader(String courseLeader) {
        this.courseLeader = courseLeader;
    }

    public int getCourseDuration() {
        return courseDuration;
    }

    public void setCourseDuration(int courseDuration) {
        this.courseDuration = courseDuration;
    }

    public boolean isCourseSandwich() {
        return courseSandwich;
    }

    public void setCourseSandwich(boolean courseSandwich) {
        this.courseSandwich = courseSandwich;
    }
}

class Student 
{
    private int studentNumber;
    private String studentName;
    private int studentPhone;
    private Course studentCourse;
    /**
     * Constructor for objects of class Student
     */
    public Student(int number, String name, int phone, Course course)
    {
        studentNumber = number;
        studentName = name;
        studentPhone = phone;
        studentCourse = course;
    }

    public int getStudentNumber() {
        return studentNumber;
    }
    public void setStudentNumber(int studentNumber) {
        this.studentNumber = studentNumber;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getStudentPhone() {
        return studentPhone;
    }
    public void setStudentPhone(int studentPhone) {
        this.studentPhone = studentPhone;
    }
    public Course getStudentCourse() {
        return studentCourse;
    }
    public void setStudentCourse(Course studentCourse) {
        this.studentCourse = studentCourse;
    }
}

class Award{
    private long awardId;
    private String awardName;

    Award(long awardId, String awardName){
        this.awardId = awardId;
        this.awardName = awardName;
    }

    public long getAwardId() {
        return awardId;
    }

    public void setAwardId(long awardId) {
        this.awardId = awardId;
    }

    public String getAwardName() {
        return awardName;
    }

    public void setAwardName(String awardName) {
        this.awardName = awardName;
    }
}

public class Test{
    public static void main(String ar[]){

        // use your all classes here


    }
}
于 2017-05-27T11:07:25.897 回答