函数的封装
//类的封装
class Demo {
public static void main(String[] args) {
//System.out.println("Hello World!");
Person p1=new Person();
p1.setAge(-24);
p1.name="lifang";
p1.speak();
Person p2=new Person();
p2.name="zhangsan";
p2.speak();
}
}
class Person {
private int age;
String name;
public int getAge(){
return age;
}
public void setAge(int a) {
if(a>0&&a<130){
age=a;
}
else
System.out.println("你输入的年龄不合法");
}
void speak(){
System.out.println("大家好,我叫"+name+"我今年"+age+"岁");
}
}
|
|