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

后端 未结 12 1645
傲寒
傲寒 2021-01-18 02:00

I\'m just working through a few things as practice for an exam I have coming up, but one thing I cannot get my head round, is using a variable that belongs to one class, in

相关标签:
12条回答
  • 2021-01-18 02:33

    There should be 3 separate objects here, a Course, a Student, and an Enrollment. An enrollment connects a Student to a Course, a Course has many Students, and a Student can enroll in many courses. None of them should extend each other.

    0 讨论(0)
  • 2021-01-18 02:34

    Here below find out the solution of your problem and if you want to check below code on your machine then create a file named Test.java and paste the below codes:

    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
    
    
        }
    }
    
    0 讨论(0)
  • 2021-01-18 02:39

    You have to declare them public.

    A better way is the keep them private, and code a public getter for that variable. for example:

    public Award getCourseAward(){
             return this.courseAward;
    }
    
    0 讨论(0)
  • 2021-01-18 02:40

    You cannot access private attributes of a class from another, this is one of the main principles of OOP: encapsulation. You have to provide access method to those attribute, you want to publish outside the class. The common approach is setter/getters - getters only, if you want to have your class immutable. Look here: http://en.wikipedia.org/wiki/Mutator_method#Java_example

    0 讨论(0)
  • 2021-01-18 02:40

    It does not make sense to arbitrarily extend classes. Student is not a Course or vice versa, so you cannot extend them like that.

    What you need to do is:

    create a Course first:

           Course aCourse = new Course(..);
    

    create a Student:

           Student aStudent = new Student(..);
    

    assign the Course to the Student:

           aStudent.setCourse(aCourse.title);
    
    0 讨论(0)
  • 2021-01-18 02:41

    First,

    You are extending Student class in Course class, which means, student class gets all the coruse class properties. So, the student class does not have the courseTitle property.

    Second, yes, it is unnesessary - you need to do the following:

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

    Example usage would be something like this:

    Course course = new Course("ASD", "TITLE", null, "ME", 50, true);   
    Student student = new Student(1, "JOHN", "5551234", course);
    

    And then, get the course information you need from student via, i.e.:

    student.studentCourse.courseTitle;
    

    Since now student.studentCourse will be a course object with all of its properties.

    Cheers,

    0 讨论(0)
提交回复
热议问题