属性:私有化 姓名,年龄,性别,学号
行为:吃,睡,学习
公共访问方法setXXX()/getXXX()
构造方法:
无参构造方法/有参构造方法
class Student{
private String name;
private int age;
private String sex;
private String id;
// 无参构造方法
public Student(){}
//有参构造
public Student(String name , int age, String sex, String id){
this.name=name;
this.age=age;
this.sex=sex;
this.id=id;
}
public void setName(String name){
this.name =name;
}
public String getName(){
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void eat (String eatWhat){
System.out.println("想吃"+eatWhat);
}
public void study (String where){
System.out.println("爱学习,上"+where+".");
}
}
public class StudentTest {
public static void main(String[] args) {
Student s = new Student ("Hiccup",20,"男","007");
System.out.println("我叫"+s.getName()+"今年"+s.getAge()+"性别"+s.getSex()+"学号"+s.getId());
s.study("千锋");
s.eat("海底捞");
}
}