
package com.guigu.exer;/* * 要求: * (1创建person类的对象,设置该对象的name、age和sex属性,调用study方法, * 输出字符串“studying”,调用shouAge()方法显示age值, * 调用addAge()方法给对象的age属性值增加2岁。 * (2创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系 */public class PersonTest { public static void main(String[] args){ person p1 = new person(); p1.name = "Tom"; p1.age = 18; p1.sex = 1; p1.study(); p1.showAge(); //p1.addAge(0); int newAge = p1.addAge(1); System.out.println(p1.name+"新年龄为:"+newAge); person p2 = new person(); p2.showAge(); p2.study(); System.out.println(p2.name+"新年龄为:"+newAge); }}public class person { String name; int sex; int age; public void study(){ System.out.println("studying"); } public void showAge(){ System.out.println("age:"+age); } public int addAge(int i){ age += i; return age; }}