|
| 1 | +//人类 |
| 2 | +classPerson { |
| 3 | +privateStringname; |
| 4 | +privateintage; |
| 5 | + |
| 6 | +publicPerson() {} |
| 7 | + |
| 8 | +publicPerson(Stringname,intage) {//"王宝强",18 |
| 9 | +this.name =name; |
| 10 | +this.age =age; |
| 11 | +} |
| 12 | + |
| 13 | +publicvoidsetName(Stringname) { |
| 14 | +this.name =name; |
| 15 | +} |
| 16 | + |
| 17 | +publicStringgetName() { |
| 18 | +returnname; |
| 19 | +} |
| 20 | + |
| 21 | +publicvoidsetAge(intage) { |
| 22 | +this.age =age; |
| 23 | +} |
| 24 | + |
| 25 | +publicintgetAge() { |
| 26 | +returnage; |
| 27 | +} |
| 28 | + |
| 29 | +publicvoideat() { |
| 30 | +System.out.println("吃饭"); |
| 31 | +} |
| 32 | + |
| 33 | +publicvoidsleep() { |
| 34 | +System.out.println("睡觉"); |
| 35 | +} |
| 36 | +} |
| 37 | +//学生类 |
| 38 | +classStudentextendsPerson { |
| 39 | +publicStudent() {} |
| 40 | + |
| 41 | +publicStudent(Stringname,intage) {//"王宝强",18 |
| 42 | +super(name,age);//"王宝强",18 |
| 43 | +} |
| 44 | +} |
| 45 | + |
| 46 | +//老师类 |
| 47 | +classTeacherextendsPerson { |
| 48 | +} |
| 49 | + |
| 50 | +classExtendsDemo5 { |
| 51 | +publicstaticvoidmain(String[]args) { |
| 52 | +//学生类 |
| 53 | +Students =newStudent(); |
| 54 | +s.setName("王祖贤"); |
| 55 | +s.setAge(27); |
| 56 | +System.out.println(s.getName()+"---"+s.getAge()); |
| 57 | +s.eat(); |
| 58 | +s.sleep(); |
| 59 | +System.out.println("--------------------------"); |
| 60 | + |
| 61 | +Students2 =newStudent("王宝强",18); |
| 62 | +System.out.println(s2.getName()+"---"+s2.getAge()); |
| 63 | +s2.eat(); |
| 64 | +s2.sleep(); |
| 65 | +System.out.println("--------------------------"); |
| 66 | + |
| 67 | + |
| 68 | +//老师类 |
| 69 | +Teachert =newTeacher(); |
| 70 | +t.setName("王祖蓝"); |
| 71 | +t.setAge(25); |
| 72 | +System.out.println(t.getName()+"---"+t.getAge()); |
| 73 | +t.eat(); |
| 74 | +t.sleep(); |
| 75 | +} |
| 76 | +} |