public class Course implements Serializable { private static final long serialiVersionUID=1L; //私有属性 private int courseId;//课程编号 private String courseName;//课程名称 private String courseType;//课程类别 private int courseCount;//课程总数 private String duration;//时长 private String teacherName;//讲师昵称 public int getCourseId() { return courseId; } public void setCourseId(int courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getCourseType() { return courseType; } public void setCourseType(String courseType) { this.courseType = courseType; } public int getCourseCount() { return courseCount; } public void setCourseCount(int courseCount) { this.courseCount = courseCount; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public Course(int courseId, String courseName, String courseType, int courseCount, String duration, String teacherName) { this.courseId = courseId; this.courseName = courseName; this.courseType = courseType; this.courseCount = courseCount; this.duration = duration; this.teacherName = teacherName; } public Course() { } @Override public String toString() { StringBuffer sb=new StringBuffer(); sb.append(this.courseId); sb.append("\t"); sb.append(this.courseName); sb.append("\t"); sb.append(this.courseType); sb.append("\t\t"); sb.append(this.courseCount); sb.append("\t"); sb.append(this.duration); sb.append("\t"); sb.append(this.teacherName); return sb.toString(); } }toString的作用
时间: 2024-02-14 18:25:36 浏览: 75
CourseDao.zip_coursedao_course属性
toString方法的作用是将对象转换为字符串表示形式。在该代码中,Course类重写了toString方法,将对象的各个属性拼接成一个字符串,并返回该字符串。
具体来说,toString方法使用StringBuffer来构建字符串,依次添加课程编号、课程名称、课程类别、课程总数、时长和讲师昵称,并使用制表符分隔它们。最后,通过调用toString方法可以获取到一个包含所有属性值的字符串表示形式。
使用toString方法可以方便地查看对象的属性值,也可以在日志输出、调试等场景中使用。
阅读全文