Person类 public class Person { String name; int age; int sex; public void study(){ System.out.println("studying"); } public void showAge(){ System.out.println("年龄为:"+age); } public int addAge(int i){ age +=i; return age; } } Person测试类 public class PersonTest { public static void main(String[] args) { Person p1 = new Person(); p1.age=10; p1.name="TOM"; p1.sex=1; p1.study(); p1.showAge(); int newAge=p1.addAge(2); System.out.println(p1.name+"的新年龄为:"+newAge); System.out.println(p1.age);//输出的是20,因为方法里已经加了2了 Person p2 = new Person(); p2.showAge();//新建的对象,堆中会新建属性,需注意,谁调方法age就是谁的 } }