完整代码如下,复制到eclipse中运行:
public class StuExam {
private String snum;// 学号
private String cnum;// 班号
private String name;// 姓名
private char sex;// 性别
private int age;// 年龄
// get/set方法用来获取/设置学生实例对象的信息
public String getSnum() {
return snum;
}
public void setSnum(String snum) {
this.snum = snum;
}
public String getCnum() {
return cnum;
}
public void setCnum(String cnum) {
this.cnum = cnum;
}
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;
}
public static void main(String[] args) {
StuExam stu = new StuExam();// 对学生类实例化
// 对学生信息赋值
stu.setSnum("0504112");
stu.setCnum("02");
stu.setSex('女');
stu.setName("杨子");
stu.setAge(20);
// 输出学生信息
System.out.println("学号:" + stu.getSnum() + "\n班号:" + stu.getCnum()
+ "\n性别:" + stu.getSex() + "\n姓名:" + stu.getName() + "\n年龄:"
+ stu.getAge());
}
}