java 一个类调用另一个类的变量,如何使用一个类的变量,在另一个类中使用Java?...

博客探讨了在面向对象编程中,如何正确处理类之间的关系。文章指出,`Course`类不应该继承`Student`类,因为一个课程不是学生。正确的做法是让学生类拥有一个`Course`类型的成员变量,表示学生参加的课程。示例代码展示了如何在`Student`类中添加课程,并提供了`attendCourse`方法来设置学生的课程。
摘要由CSDN通过智能技术生成

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.

Here is my Course class:

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;

}

}

And here is Student:

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;

}

}

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

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!

Thanks!

解决方案

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

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).

Here is a sample code:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值