class BigPerson
{
private String name;
private int age;
void talk()
{
System.out.println("My name is "+ name +" and my age is "+age);
}
public void setName(String string)
{
name = string;
}
public void setAge(int aage)
{
if(aage>=0)
age = aage;
}
public String getNameString()
{
return name;
}
public int getAge()
{
return age;
}
}
public class Person {
public static void main(String args[])
{
BigPerson person = new BigPerson();
person.setAge(-15);
person.setName("Hou");
person.talk();
}
}
BigPerson person = new BigPerson();声明并实例化一个BigPerson对象
如果此时将talk()方法设置为private:
private void talk()
{
System.out.println("My name is "+ name +" and my age is "+age);
}
同时再添加一个say()方法:
void say()
{
this.talk();
}
便是在类内部调用方法。
此时person.talk();改为person.say();