- 定义父类的格式
- public class 父类名称{ }
- 定义子类的格式:
- public class 子类名称 extends 父类名称{ }
实例:
public class Employee {
public void method() {
System.out.println("方法执行!");
}
}
public class Teacher extends Employee{
}
public class Assistant extends Employee{
}
public class ExtendsDemo01 {
public static void main(String[] args) {
Teacher teacher = new Teacher();
//Teacher类中虽然什么都没写,但是会继承来自父类的method方法
teacher.method();//方法执行!
Assistant assistant = new Assistant();
assistant.method();
}
}
运行结果:
方法执行!
方法执行!