A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. [子类从其父类继承所有成员(字段,方法和嵌套类)。 构造函数不是成员,所以它们不被子类继承,但是可以从子类调用超类的构造函数。]
来自Oracle官方文档https://docs.oracle.com/javas...
class Parent{
Parent() {
System.out.println("调用父类构造方法!");
}
private static void staticParent() {
System.out.println("调用父类静态方法");
}
private final void finalParent() {
System.out.println("调用父类final方法");
}
private void printParent(){
System.out.println("调用父类私有方法");
}
}
class Child extends Parent {
public void printChild(){
System.out.println("调用子类公有方法");
}
}
public class Test {
public static void main(String[] args) throws Exception {
//获取子类
Class clazz = Class.forName("work.litao.Child");
//得到父类
Class superClass = clazz.getSuperclass();
//得到父类非继承的所以方法
Method[] methods = superClass.getDeclaredMethods();
//设置私有方法可以被访问
AccessibleObject.setAccessible(methods,true);
for (Method m:methods) {
System.out.println();
System.out.println("子类调用方法"+m.getName()+"()的调用结果:" );
m.invoke(new Child());
}
}
}
运行结果:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。