簡體   English   中英

如何在Java中使用另一類的變量?

[英]How to use a variable of one class, in another in Java?

我正在為即將參加的考試做一些練習,但是我無法理解的一件事是使用屬於一個班級,屬於另一個班級的變量。

我有一個課程班和一個學生班。 課堂課程存儲所有不同的課程,而我只是想做的就是使用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;
    }

}

我在課程中使用“ 擴展 ”是否正確? 還是這是不必要的?

在我的Student構造函數中,我試圖將Course類中的“ courseTitle”分配給變量“ studentCourse”。 但是我根本不知道該怎么做!

預先感謝您的幫助,期待您的回音!

謝謝!

我在課程中使用“擴展”是否正確? 還是這是不必要的?

不幸的是,如果您想知道您的繼承是否正確,請使用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);

我認為課程不是學生,因此在這些課程之間進行繼承可能不是一個好主意。

您必須宣布它們為公開。

更好的方法是將它們設為私有,並為該變量編寫公共獲取程序。 例如:

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

Course不應擴展Student 如果要訪問CoursecourseTitle字段,則需要courseTitle Course對象的引用傳遞給Student ,然后執行course.CourseTitle。

您不能從另一個類訪問一個類的私有屬性,這是OOP的主要原則之一:封裝。 您必須提供對那些屬性的訪問方法,並且要在類外部發布。 通用的方法是setter / getters-僅當想要使類不可變時才使用getter。 在這里查看: 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個單獨的對象,一個課程,一個學生和一個注冊。 一個注冊將一個學生連接到一個課程,一個課程有許多學生,並且一個學生可以注冊許多課程。 它們都不應該相互延伸。

也許您不需要將課程名稱添加到學生。 我要做的是將“學生”添加到“課程”中的某些數據結構中。 這樣更清潔,減少了課程和學生之間的耦合。 這也使您可以讓學生參加多個課程。 例如:

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

}

第一,

您要在“課程”類中擴展“學生”類,這意味着,“學生”類將獲得所有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將成為具有所有屬性的課程對象。

干杯,

如前所述,請遠離“擴展”。 通常,除非“ is-a”關系有意義,否則不應使用它。

您可能應該為Course類上的方法提供吸氣劑:

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

然后,如果Student類需要它,它將以某種方式掌握課程(這取決於您的設計),然后調用getter:

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

在下面找到問題的解決方案,如果要在計算機上檢查以下代碼,請創建一個名為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


    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM