1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class TestThisKeyword2 { public static void main(String[] args) { Teacher t1 = new Teacher("Jerry", 30, '男', 15000.0); System.out.println(t1.name); System.out.println(t1.age); System.out.println(t1.sex); System.out.println(t1.salary); } }
class Teacher { String name; int age; char sex; double salary; public Teacher() {} public Teacher(String name, int age, char sex) { this.name = name; this.age = age; this.sex = sex; System.out.println("3参构造执行完毕!"); } public Teacher(String name, int age, char sex, double salary) { this(name, age, sex); this.salary = salary; System.out.println("4参构造执行完毕!!"); } } 输出: 3参构造执行完毕! 4参构造执行完毕!! Jerry 30 男 15000.0
|