Java learning - inherited format

/ *
Inheritance relationship, "sub-class is a parent class." That is, the subclass can be viewed as a parent.
For example, the parent is an employee, a subclass is a lecturer, then the "instructor is an employee." Relationship: is-a.

Format definition :( a common parent class of class definitions)
public class the parent class name {
// ...
}

Format defined subclasses:
public class name of the subclass extends the parent class name {
// ...
}
* /

public class Demo01Extends {
    public static void main(String[] args) {
        // 创建了一个子类对象
        Teacher teacher = new Teacher();
        // Teacher类当中虽然什么都没写,但是会继承来自父类的method方法。
        teacher.method();

        // 创建另一个子类助教的对象
        Assistant assistant = new Assistant();
        assistant.method();
    }
}
// 定义一个父类:员工
public class Employee {

    public void method() {
        System.out.println("方法执行!");
    }

}
// 定义了一个员工的子类:讲师
public class Teacher extends Employee {

}
// 定义了员工的另一个子类:助教
public class Assistant extends Employee {
}
Published 23 original articles · won praise 0 · Views 142

Guess you like

Origin blog.csdn.net/qq_44813352/article/details/104327386