私はちょうど私が来る試験の練習としていくつかのことをやっていますが、私は頭がいっぱいではありませんが、別のクラスの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'に割り当てようとしています。しかし、私は単にこれを行う方法を理解することはできません!
ご協力いただきありがとうございます。私はあなたのご意見をお待ちしております!
ありがとうございます!
このケースでは、多対多マッピングの結合テーブル(登録オブジェクト)が余分に必要になるとは思われません。 (これは伝統的なデータベースに残されていなければならない) –
@ジョン:はい、OPは明らかにOOを悪用するという恐ろしい例にさらされているので、これをモデリングするためのより完全な選択肢を示したい。 –
が当てはまります。 –