package test;public class Person { String name; int age; public Person() { System.out.println("无参数构造方法"); } public Person(String name) { this(); this.name = name; System.out.println("姓名:"+name); } public Person(String n, int a) { this(n); this.age = a; System.out.println("姓名:"+name+" "+"年龄:"+age); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void getName() { System.out.println("姓名:"+name); } public void getAge() { System.out.println("年龄:"+age); } public static void main(String[] args) { Person p = new Person("张三",20); p.getAge(); p.setAge(30); p.getAge(); }}
输出结果
无参数构造方法姓名:张三姓名:张三 年龄:20年龄:20年龄:30