/*
人类Person
属性:姓名name,年龄age,性别gender
行为:学习study,睡觉sleep
*/
class Person_Test001 {
public static void main(String[] args) {
Person p = new Person();
p.name = "张三丰";
p.setAge(100);
System.out.println(p.name+"..."+p.getAge());
p.study();
p.sleep();
}
}
class Person {
String name;
private int age;
//封装
public void setAge(int a) {
if (a > 0 && a < 150) {
age = a;
}else {
System.out.println("地球不是和你,还是回火星吧");
}
}
public int getAge() {
return age;
}
String gender;
public void study() {
System.out.println("学习");
}
public void sleep() {
System.out.println("睡觉");
}
}
|
|