Person类
package com.ITheima.d7_extends; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }View Code
Student类继承Person类
package com.ITheima.d7_extends; public class Student extends Person{ public void study(){ System.out.println(getName()+"好好学习"); } }View Code
Teacher类继承Person类
package com.ITheima.d7_extends; public class Teacher extends Person { public void teach() { System.out.println(getName()+"开始教导学生"); } }View Code
案例