如何在Java中使用另一个类的变量?

我正在做一些事情作为我即将到来的考试的练习,但有一件事我无法理解,是使用属于一个类的变量,在另一个类中。

我有一个Course课程和一个Student课程。 课程存储所有不同的课程,我只想要能够做的是在课堂学生中使用课程的名称。

这是我的课程课程:

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; } } 

我在课程中使用’ extends ‘是否正确? 或者这是不必要的?

在我的学生构造函数中,我试图将类课程中的’courseTitle’分配给变量’studentCourse’。 但我根本想不通怎么做!

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

谢谢!

我在课程中使用’extends’是否正确? 或者这是不必要的?

不幸的是,如果您想知道您的inheritance是否正确,请使用is-a替换extends 。 课程是学生吗? 答案是不。 这意味着您的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); 

我假设课程不是学生,所以这些课程之间的inheritance可能是一个坏主意。

你必须公开他们。

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

 public Award getCourseAward(){ return this.courseAward; } 

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

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

任意扩展类是没有意义的。 学生不是课程,反之亦然,所以你不能像那样扩展课程。

你需要做的是:

首先创建一个课程:

  Course aCourse = new Course(..); 

创建学生:

  Student aStudent = new Student(..); 

将课程分配给学生:

  aStudent.setCourse(aCourse.title); 

使用Couse 扩展 Student ,因为他们不是同一类型。 当专门化一个更普遍的(在某种意义上)时,会将一个类扩展到另一个类。
解决方案是将courseTitle作为Student构造函数的参数传递

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

第一,

您正在课程类中扩展Student类,这意味着,student类获取所有coruse类属性。 因此,学生class级没有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将成为具有所有属性的课程对象。

干杯,

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

 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 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(); } public boolean addStudent(Student student){ return students.add(student); } public Set getStudents(){ return students; } 

}

如上所述,远离“延伸”为此。 一般情况下,除非“is-a”关系有意义,否则不应使用它。

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

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

然后,如果学生课程需要,那么它会以某种方式掌握课程(这取决于你的设计),并调用getter:

 public class Student { private Set courses = new HashSet(); public void attendCourse(Course course) { courses.add(course); } public void printCourses(PrintStream stream) { for (Course course : courses) { stream.println(course.getTitle()); } } } 

下面找到问题的解决方案,如果要在机器上检查下面的代码,则创建一个名为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 } }