如何使用一个类的变量,在另一个类中使用Java? [英] How to use a variable of one class, in another in Java?

查看:302
本文介绍了如何使用一个类的变量,在另一个类中使用Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在做一些事情,作为一个考试的练习我来了,但有一件事我不能得到我的头,使用一个变量属于一个类,在不同的类。

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 a different class.

我有一个课程类和一个学生类。课程存储所有不同的课程,我只想要能够做的是使用课程的名称,在类学生。

I have a Course class and a Student class. Class course stores all the different courses and what I simply want to be able to do is use the name of the course, in class 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;
    }

}

strong>延伸?或者这是不必要的?

Am I correct in using 'extends' within Course? Or is this unnecessary?

在我的Student构造函数中,我试图将CourseTitle从课程中分配给变量studentCourse。但我根本无法想象如何做到这一点!

In my constructor for Student, I am trying to assign 'courseTitle' from class Course, to the variable 'studentCourse'. But I simply cannot figure how to do this!

感谢您的帮助,期待您的回音!

Thank you in advance for your help, I look forward to hearing from you!

谢谢!

推荐答案


我在课程中使用'extend'或者这是不必要的?

Am I correct in using 'extends' within Course? Or is this unnecessary?

不幸的是,如果你想知道你的继承是否正确, / strong>,其中包含 is-a 。课程是学生?答案是不。这意味着课程不应扩展学生

Unfortunately not, if you want to know whether your inheritance is correct or not, replace extends with is-a. A course is a student? The answer is no. Which means your Course should not extend Student

学生可以参加课程,因此学生类可以有类的成员变量。如果您的模型指定了(学生可以参加几门课程),您可以定义课程列表。

A student can attend a Course, hence the Student class can have a member variable of type Course. You can define a list of courses if your model specifies that (a student can attend several courses).

以下是示例代码:

public class Student{
    //....
    private Course course;
    //...
    public void attendCourse(Course course){
       this.course = course;
    }
    public Course getCourse(){
       return course;
    }
}

现在,您可以拥有以下内容:

Now, you can have the following:

Student bob = new Student(...);
Course course = new Course(...);
bob.attendCourse(course);

这篇关于如何使用一个类的变量,在另一个类中使用Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆