class Demo1_Person {
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "张三"; //调用姓名属性并赋值
//p1.age = -17; //调用年龄属性并赋值
//p1.speak(); //调用行为
p1.setAge(-17);
System.out.println(p1.getAge());
}
}
class Person {
String name; //姓名
private int age; //年龄
public void setAge(int a) { //设置年龄
if (a > 0 && a < 200) {
age = a;
}else {
System.out.println("请回火星吧,地球不适合你");
}
}
public int getAge() { //获取年龄
return age;
}
public void speak() {
System.out.println(name + "..." + age);
}
} |
|