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

时间:2012-06-28 19:30:43

标签: java class variables call

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

我有一个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”。但我根本想不通怎么做!

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

谢谢!

12 个答案:

答案 0 :(得分:11)

  

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

不幸的是,如果您想知道您的继承是否正确,请将 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);

答案 1 :(得分:4)

我认为课程不是学生,所以这些课程之间的继承可能是一个坏主意。

答案 2 :(得分:4)

你必须将它们公开。

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

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

答案 3 :(得分:2)

Course不应展开Student。如果您要访问courseTitle的{​​{1}}字段,则需要将对Course对象的引用传递给Course,然后执行course.CourseTitle。

答案 4 :(得分:2)

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

答案 5 :(得分:2)

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

您需要做的是:

首先创建课程:

       Course aCourse = new Course(..);

创建学生:

       Student aStudent = new Student(..);

将课程分配给学生:

       aStudent.setCourse(aCourse.title);

答案 6 :(得分:2)

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

的参数传递

答案 7 :(得分:2)

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

答案 8 :(得分:2)

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

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

}

答案 9 :(得分:2)

首先,

您正在课程课程中扩展学生课程,这意味着,学生课程将获得所有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将成为具有所有属性的课程对象。

干杯,

答案 10 :(得分:1)

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

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

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

然后,如果学生班需要,那么它会以某种方式掌握课程(这取决于你的设计),并打电话给吸气者:

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

答案 11 :(得分:1)

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

package 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


    }
}