package p1; /** * @author WanJl * @version 1.0 * @title p1.Person * @description * @create 2026/7/20 */ public class Person{ //程序 服务 String name; //给age设置为私有private。除了本类之外的任何类都不能访问 private int age; //定义一个public修饰的用来给age赋值的方法 /** * 为age赋值的方法 * @param newAge */ public void setAge(int newAge){ if (newAge>=1&&newAge<130){ age=newAge; }else { System.out.println("年龄超过规定限制,赋值无效..."); } } /** * 获取age属性的方法 * @return */ public int getAge(){ return age; } }