1.编写一个JAVA类程序片段,定义一个表示学生的类student,包括域“学号”、“班号”、“姓名”、“年龄”;方法“获得学号”、“获得班号”、“获得性别”、“获得姓名”、“获得年龄”、“修改年龄”
2.在上一道题的基础上编写Java Application程序创建student类的对象
//
//第一题
//
public class Student {
private String id;//学号
private int classId;//班号
private String name;//姓名
private String sex;//性别
private int age;//年龄
//设置学生id
public void setId(String id) {
this.id = id;
}
//获取学生id
public String getId() {
return id;
}
//设置班级id
public void setClassId(int classId) {
this.classId = classId;
}
//获取班级ID
public int getClassId() {
return classId;
}
//设置年龄
public void setAge(int age) {
this.age = age;
}
//获取年龄
public int getAge() {
return age;
}
//设置姓名
public void setName(String name){
this.name = name;
}
//获取姓名
public String getName() {
return name;
}
//设置性别
public void setSex(String sex) {
this.sex = sex;
}
//获取性别
public String getSex() {
return sex;
}
//重写toString()
public String toString() {
return "学生号:"+id+" 班级号:"+classId+
" 姓名:"+name+" 性别:"+sex+" 年龄:"+age;
}
public static void main(String args[]) {
Student s = new Student();
s.setId("201190609112");
s.setClassId(111-1);
s.setName("张星龙");
s.setSex("男");
s.setAge(21);
System.out.println(s);//打印内容为重写toString()后的内容
}
}
//
//第2题
//
public class Chuangjian {
public static void main(String args[]) {
Student demoStudent = new Student();
System.out.print("学生号:" + demoStudent.getStudentId() + " 班级号:" + demoStudent.getclassId() +
" 姓名:" + demoStudent.getStudentName() + " 性别:" + demoStudent.getStudentSex() + " 年龄:"+demoStudent.getStudentAge() );
}
}
class Student {
private String studentId="201190609112";
private int classId=1111;
private String studentName="张星龙";
private char studentSex='男';
private int studentAge=21;
String getStudentId() {
return studentId;
}
int getclassId() {
return classId;
}
char getStudentSex() {
return studentSex;
}
String getStudentName() {
return studentName;
}
int getStudentAge() {
return studentAge;
}
}
运行效果: