class Person
{
private int age;
private String name;
public Person(String name,int age)//构造方法为属性初始化
{
this.setAge(age);
this.setName(name);
}
public void tell()
{
System.out.println("name:"+getName()+"age:"+getAge());
}
public String getName()
{
return name;
}
public void getName(String n)
{
name = n;
}
public int getAge()
{
return age;
}
public void setAge(int a)
{
if(a>0 && a<100)
age = a;
}
}
public class TestEncapsulation4
{
public static void main(String [] args)
{
Person per1 = new Person("陈思思",33); //对象调用构造方法初始化类的属性
per1.tell();
}
}
感觉没有问题,可是。。。劳各位技术大牛没帮我看看。谢谢。