public class Encapsulation_1{
public static void main(String[] args){
person Z=new person();
Z.setAge(25);
System.out.println(Z.getAge());
new person().setAge(23);
Z.speak();
}
}
class person
{
private int age;
public void setAge(int a)
{
if(a>0&&a<150)
{
age=a;
}
else
System.out.println("非法年龄");
}
public int getAge()
{
return age;
}
void speak()
{
System.out.println("年龄是"+age);
}
}
问题 class person中
void speak()
{}
前面加public与不加有没有区别? |