用JAVA编写一个学生类Student的程序
2. 定义一个测试类StudentTest创建两个学生对象,John,Mark,通过新建对象传值初始化对象属性,分别显示两个学生的学号,性别,年龄,然后修改John的年龄并显示之。 展开
package langs;
public class Student {
private String sno;
private String name;
private char sex;
private int age;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String sno, String name, char sex, int age) {
super();
this.sno = sno;
this.name = name;
this.sex = sex;
this.age = age;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package langs;
public class StudentTest {
public static void main(String[] args) {
Student john = new Student("s001","john",'男',24);
Student mark = new Student("s002","mark",'男',25);
System.out.println("john学号: " + john.getSno());
System.out.println("mark学号: " + mark.getSno());
System.out.println("john姓名: " + john.getName());
System.out.println("mark姓名: " + mark.getName());
System.out.println("john年龄: " + john.getAge());
System.out.println("mark年龄: " + mark.getAge());
john.setAge(26);
System.out.println("john年龄: " + john.getAge());
}
}
学生类:
public class Student {
private String num;//学号
private String name;//姓名
private String sex;//性别
private int age;//年龄
public Student(String num,String name,String sex,int age){
this.num = num;
this.name = name;
this.sex = sex;
this.age = age;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试类:
public class StudentTest {
public static void main(String[] args) {
Student John = new Student("0000026", "John", "男", 18);//John
Student Mark = new Student("0000027", "Mark", "男", 20);//Mark
System.out.println(John.toString());//显示是输出?
System.out.println(Mark.toString());
John.setAge(22);//修改John年龄
System.out.println("修改后的John:"+John.toString());
}
}