java语法随笔

xiaoxiao2021-02-27  399

执行顺序 父类静态代码块->子类静态代码块->父类动态代码块->父类构造方法->子类动态代码块->子类构造方法 父类对象指向子类对象,只能调用父类中定义的方法,如被重写,则执行子类的方法,如果调用没有定义的方法,则编译出错。

class Father{ private String name = "Father"; static { System.out.println("Father static block"); } { System.out.println("Father block"); } public Father(){ System.out.println("Father constructor"); } public void test(){ System.out.println("test Father"); } public void f(){ System.out.println("f()"); } } public class Son extends Father{ public String name= "Son"; static { System.out.println("Son static block"); } { System.out.println("Son block"); } public Son(){ System.out.println("Son constructor"); } public void s(){ System.out.println("s()"); } public static void main(String[] args){ new Son(); Father son = new Son(); //System.out.println(son.name); son.f(); //son.s(); } }
转载请注明原文地址: https://www.6miu.com/read-3898.html

最新回复(0)