Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中的另一个类中使用一个类的变量?_Java_Class_Variables_Call - Fatal编程技术网

如何在Java中的另一个类中使用一个类的变量?

如何在Java中的另一个类中使用一个类的变量?,java,class,variables,call,Java,Class,Variables,Call,我只是在为即将到来的考试做一些练习,但有一件事我无法理解,那就是使用一个属于不同类的变量 我有一个课程班和一个学生班。课堂课程存储了所有不同的课程,我只想在课堂上使用课程的名称 这是我的课程: public class Course extends Student { // instance variables - replace the example below with your own private Award courseAward; private Stri

我只是在为即将到来的考试做一些练习,但有一件事我无法理解,那就是使用一个属于不同类的变量

我有一个课程班和一个学生班。课堂课程存储了所有不同的课程,我只想在课堂上使用课程的名称

这是我的课程:

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 Course {
   ...
   public String getTitle() {
       return title;
   }
}
学生们:

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构造函数中,我试图将class-Course中的'CourseTile'赋值给变量'studentCourse'。但我根本不知道该怎么做

提前感谢您的帮助,我期待着您的回复


谢谢

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

你必须将它们公开

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

public Award getCourseAward(){
         return this.courseAward;
}
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;
} 

课程
不应扩展
学生
。如果要访问
课程
课程
字段,需要将对
课程
对象的引用传递给
学生
,然后执行Course.courseTitle。

不能从另一个类访问类的私有属性,这是OOP的主要原则之一:封装。如果要在类外部发布,则必须提供这些属性的访问方法。如果希望类不可变,常用的方法是setter/getter-getter-only。看这里:

我在课程中使用“扩展”正确吗?或者这是没有必要的

不幸的是,如果您想知道您的继承是否正确,请将extends替换为is-a。一门课程是一个学生吗?答案是否定的。这意味着你的
课程
不应该扩展
学生

学生可以参加
课程
,因此
学生
类可以有
课程
类型的成员变量。如果模型指定(一个学生可以参加多个课程),则可以定义课程列表

下面是一个示例代码:

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 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 aCourse = new Course(..);
创建一个学生:

       Student aStudent = new Student(..);
将课程分配给学生:

       aStudent.setCourse(aCourse.title);

Couse
扩展学生,因为他们不是同一类。用另一个类扩展一个类是在专门化一个更一般(某种意义上)的类时发生的。

解决方案是将
CourseTile
作为
Student
构造函数的参数传递这里应该有3个独立的对象,一个课程、一个学生和一个注册。注册将一个学生连接到一门课程,一门课程有许多学生,一个学生可以注册许多课程。它们都不应相互扩展。

也许您不需要将课程名称添加到学生。我要做的是在课程中将学生添加到一些数据结构中。这样更干净,减少了课程和学生之间的耦合。这也允许学生参加多个课程。例如:

public Award getCourseAward(){
         return this.courseAward;
}
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;
} 
公共课程扩展学生{
私人奖励课程;
私有字符串代码;
公共弦乐;
private Student courseLeader;//更改为学生对象
私立国际课程教育;
私人布尔课程桑德维奇;
私人设置学生;//让课程容纳学生集合
/**
*课程对象的构造函数
*/
公共课程(字符串代码、字符串标题、奖励、学生领袖、整数持续时间、布尔三明治){
courseCode=代码;
课程名称=头衔;
课程前进=奖励;
courseLeader=领导者;
课程教育=持续时间;
courseSandwich=三明治;
this.students=newhashset();
}
公立学校学生(学生){
返回学生。添加(学生);
}
公共集getStudents(){
留学生;
} 
}首先

您正在课程类中扩展Student类,这意味着Student类将获得所有coruse类属性。因此,学生类没有CourseTile属性

其次,是的,这是不必要的-您需要执行以下操作:

Student bob = new Student(...);
Course course = new Course(...);
bob.attendCourse(course);
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”关系有意义,否则不应该使用它

您可能应该为课程类上的方法提供getter:

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 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());
       }
   }
}
公共班级学生{
私有集课程=新HashSet();
公共缺席课程(课程){
课程。添加(课程);
}
公共作废打印课程(打印流){
用于(课程:课程){
stream.println(course.getTitle());
}
}
}

在下面找到问题的解决方案,如果要检查机器上的以下代码,请创建一个名为Test.java的文件并粘贴以下代码:

包装组件

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


    }
}

我认为在这种情况下不需要多对多映射联接表(注册对象)的额外层。(除非这需要保存在传统数据库中)@John:是的,但是由于OP显然暴露了一些滥用OO的可怕例子,我更愿意展示一个更具挑战性的例子