封装以及构造方法的实例
作者:Java伴侣 日期:2006-06-02
class Person
{
private int age;
private String name;
void shout()
{
if(age<0)
return;
System.out.println(name+"&aposs age is"+age);
}
public Person()
{
}
public Person(String name)
{
this.name=name;
}
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void getSomeOne(Person p)
{
p.shout();
}
public static void main(String [] args)
{
Person p1=new Person("lisi");
Person p2=new Person("zangsan",20);
Person p3=new Person("wangwu");
p1.age=-30;
p1.shout();
p2.shout();
p3.shout();
}
}
输出结果: