问题描述
定义一个学生的类 Student,包括成员变量“学号”、“班号”、“姓名”、“性别”、“年龄” ;成员方法:“获得学号”、“获得班号”、“获得性别”、“获得姓名”、“获得年龄”。请为 Student 类定义构造函数初始化所有的成员变量。增加一个方法 public String toString()把 Student 类对象的所有信息组合成一个字符串。编写测试类,创建学生类对象,调用toString()将该对象的所有信息在屏幕上进行显示。
解决方案
public class Student {public String name;public String sex;public int age;public String stu_num;public String class_num;public Student(){}public Student(String name,String sex,int age,String stu_num,String class_num){this.name = name;this.sex = sex;this.age = age;this.stu_num = stu_num;this.class_num = class_num;}public String getSex() {return sex;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public void setStu_num(String stuNum) {stu_num = stuNum;}public void setClass_num(String classNum) {class_num = classNum;}public String toString(){return "name:"+this.name+"sex: "+this.sex+"age: "+this.age+"stu_num: "+this.stu_num+"class_num: "+this.class_num;}public static void main(String args[]){Student s = new Student("1","2",3,"4","5");System.out.println(s.toString());}}
解决方案二:
自己动手,丰衣足食。