#为什么我用User类调用Student的display方法时候,Course传给我的是一个地址。
学生对象类
package com.powernode.javase.oop_08;
public class Student {
private String name;
private String no;
private Course course;
public void selection(Course course){
this.setCourse(course);
System.out.println(this.getName()+"选课成功:"+getCourse());
}
public void cancel(){
String courseName = this.getCourse().getName();
this.setCourse(null);
System.out.println(this.getName() + "退课成功:" + courseName);
}
public void display() {
System.out.println("名字:" + getName() + " id" + getNo() + " 课程" + getCourse());
}
public Student(String name, String no) {
this.name = name;
this.no = no;
}
public Student(String name, String no,Course course) {
this.name = name;
this.no = no;
this.course = course;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
课程对象类
package com.powernode.javase.oop_08;
public class Course {
private String name;
private String teacher;
public void display(){
System.out.println("课程名称:" + this.getName() + "授课老师:" + this.getTeacher());
}
public Course(String name, String teacher) {
this.name = name;
this.teacher = teacher;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}
User类
package com.powernode.javase.oop_08;
public class Use {
public static void main(String[] args) {
Course c = new Course("java","杜老师");
Student s = new Student("小白","123",c);
s.display();
}
}
运行结果
名字:小白 id123 课程com.powernode.javase.oop_08.Course@1b6d3586