定义一个 Student类,其中包括以下要求 a.私有属性:学号、姓名、性别、出生年月; b.建立构造方法初始化各属性; c.为每个属性建立 Getters和 Setters方法 d.建立方法 di splay0—显示 Student对象的所有属性 e.实现并测试这个类
一个实现:
/**
*
*/
package ask.csdn.net;
public class Student {
private int id;
private String name;
private String sex;
private String birthday;
public Student(int id, String name, String sex, String birthday) {
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public void display() {
System.out.println("学生信息-学号:"+getId()+",姓名:"+getName()
+",性别:"+getSex()+",出生年月:"+getBirthday());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student(111,"张三","男","1993-09");
s.display();
}
}
稍微动动手,自己创建一下,把不会的问题拿出来
熟练敲代码了以后,get和set方法,以及构造方法等在eclipse中都可以自动生成。