2012-06-28 10 views
6

私はちょうど私が来る試験の練習としていくつかのことをやっていますが、私は頭がいっぱいではありませんが、別のクラスの1つのクラスに属する変数を使用しています。あるクラスの変数をJavaの別のクラスで使用する方法は?

私は、CourseクラスとStudentクラスを持っています。クラスコースはすべての異なるコースを格納しています。私が単にやりたいことを望むのは、コースの名前をクラス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; 
    } 

} 

私は 'を拡張して' コース内を使用して修正するのですか?それともこれは不要ですか?

私の生徒用コンストラクタでは、クラスcourseから 'courseTitle'を変数 'studentCourse'に割り当てようとしています。しかし、私は単にこれを行う方法を理解することはできません!

ご協力いただきありがとうございます。私はあなたのご意見をお待ちしております!

ありがとうございます!

答えて

4

私はコースを受け取りますではないので、それらのクラス間の継承はおそらく悪い考えです。

4

公開する必要があります。

より良い方法は、それらをプライベートに保ち、その変数のパブリックゲッターをコード化することです。例えば:

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

CourseStudentを拡張するべきではありません。 CoursecourseTitleフィールドにアクセスする場合は、Courseオブジェクトへの参照をStudentに渡してから、course.CourseTitleを実行する必要があります。

2

クラスのプライベート属性に別のものからアクセスすることはできません。これはOOPの主な原則の1つです:カプセル化。それらの属性へのアクセス方法を提供しなければなりません。あなたはクラス外で公開したいのです。一般的なアプローチは、クラスを不変にしたい場合は、setter/getters-gettersのみです。ここをクリックしてください:http://en.wikipedia.org/wiki/Mutator_method#Java_example

11

コース内で 'extend'を使用すると正しいですか?それともこれは不要ですか?残念ながら

ないあなたの継承が正しいかどうかを知りたい場合は、を交換がある-拡張します。コースは学生ですか?答えはいいえだ。これはあなたのCourseStudent

学生したがってStudentクラスはタイプCourseのメンバ変数を持つことができ、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

任意のクラスを拡張しても意味がありません。学生はコースではなく、その逆もありますので、そのように拡張することはできません。

何をする必要がある:

は、最初にコースを作成します。

 aStudent.setCourse(aCourse.title); 

 Student aStudent = new Student(..); 

が学生にコースを割り当てる:

 Course aCourse = new Course(..); 

は学生を作成します

2

StudentCouseに拡張すると、同じ種類ではないためです。あるクラスを別のクラスに拡張することは、より一般的な(ある意味で)特殊化するときに起こります。
ソリューションはStudentコンストラクタの引数としてcourseTitleを渡すことであろう

2

ここに3つの別々のオブジェクト、コース、学生、および登録があるはずです。学生はコースに登録し、コースには学生が多く、学生は多くのコースに登録することができます。それらのどれもお互いを広げてはならない。

+0

このケースでは、多対多マッピングの結合テーブル(登録オブジェクト)が余分に必要になるとは思われません。 (これは伝統的なデータベースに残されていなければならない) –

+0

@ジョン:はい、OPは明らかにOOを悪用するという恐ろしい例にさらされているので、これをモデリングするためのより完全な選択肢を示したい。 –

+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クラスのプロパティを取得します。したがって、studentクラスには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.studentCourseはコースになりますすべてのプロパティを持つオブジェクト

乾杯、

1

前述したように、このために「拡張」から離れてください。一般に、 "is-a"関係が理にかなっていない限り、使用しないでください。

あなたはおそらくコースのクラスのメソッドのゲッターを提供する必要があります:Studentクラスは、それが何らかの形であなたにあなた次第である(もちろんのホールドになるだろう、という必要がある場合、その後

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 


    } 
} 
関連する問題