class Demo_Person {
public static void main(String[] args) {
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("妖");
System.out.println(p.getName() + "..." + p.getAge() + ".." + p.getGender());
Person p1 = new Person("李四",24,"女");
System.out.println(p1.getName() + "..." + p1.getAge() + ".." + p1.getGender());
p.study();
p.sleep();
}
}
class Person {
private String name; //成员变量
private int age;
private String gender;
public Person(){} //空参构造
public Person(String name,int age,String gender) {//有参构造
this.name = name;
this.age = age;
this.gender = gender;
}
public void setName(String name) { //set和get方法
this.name = name;
}
public String getName() {
return this.name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGender() {
return this.gender;
}
public void study() { //吃饭方法
System.out.println("学习");
}
public void sleep() {
System.out.println("睡觉"); //睡觉方法
}
}
|
|