2012-06-28 136 views
6

我只是通过一些练习来练习我正在练习的练习题,但有一件事我无法理解,就是在一个不同的班级中使用属于一个班级的变量。如何在Java的另一个类中使用一个类的变量?

我有课程课程和学生课程。班级课程存储所有不同的课程,我只想在班级学生中使用该课程的名称。

这是我的课程类:

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

} 

我是在利用场内 '延伸' 正确吗?或者这是不必要的?

在我的学生构造函数中,我试图将课程'courseTitle'分配给变量'studentCourse'。但我根本无法想象如何做到这一点!

非常感谢您的帮助,我期待您的回音!

谢谢!

回答

4

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

4

你必须声明它们是公开的。

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

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

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

2

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

11

我正确使用'延伸'课程?或者这是不必要的?

不幸的是没有,如果你想知道你的产业是否正确与否,更换扩展是-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); 
2

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

你需要做的是:

首先创建一个课程:

 Course aCourse = new Course(..); 

创建一个学生:

 Student aStudent = new Student(..); 

课程分配给学生:

 aStudent.setCourse(aCourse.title); 
2

扩展StudentCouse因为它们不是同一种。当专门研究更一般的(某种意义上的)类时,扩展一个类与另一个类会发生。
解决办法是通过courseTitleStudent构造函数的参数

2

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

+0

我不认为你需要这种情况下的多对多连接表(登记对象)的额外层。 (除非这需要在传统数据库中保留) –

+0

@John:是的,但是由于OP显然已经暴露于某些滥用面向对象的可怕示例,所以我宁愿展示一个更为彻底的模型替代方案。 –

+0

这是有道理的。 –

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

}

2

首先,

您在课程类,这意味着扩大学生类,学生类获取所有coruse类属性。所以,学生课没有courseTitle属性。

其次,是的,这是unnesessary - 你需要做到以下几点:

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.student课程将成为课程具有所有属性的对象。

干杯,

1

如上所述,远离“延伸”这一点。一般来说,除非“是 - 一个”关系合理,否则不应该使用它。

你或许应该对课程类的方法提供干将:

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


    } 
} 
相关问题