zoukankan      html  css  js  c++  java
  • java 继承类 变量、静态变量、构造函数执行顺序

    java 继承类 变量、静态变量、构造函数执行顺序 

    Java代码  收藏代码
    1. class C{  
    2.     static int prt(int i){  
    3.       System.out.println(i);  
    4.       return i;  
    5.     }  
    6. }  
    7. class A{  
    8.     int a=C.prt(0);  
    9.     static int b=C.prt(1);  
    10.     A(){  
    11.       System.out.println("constructor of A");  
    12.     }  
    13. }  
    14. class B extends A{  
    15.     int c=C.prt(2);  
    16.     static int d=C.prt(3);  
    17.     B(){  
    18.         System.out.println("constructor of B");  
    19.     }  
    20.     public void test(){  
    21.         System.out.println("begin...");  
    22.     }  
    23. }  
    24. public class OrderTest{  
    25.     public static void main(String[] args){  
    26.         B b=new B();  
    27.         b.test();  
    28.     }  
    29. }  
    30.   
    31. 执行结果:  
    32.   
    33. 1  
    34. 3  
    35. 0  
    36. constructor of A  
    37. 2  
    38. constructor of B  
    39. begin...  



    可以看出包含普通变量、静态变量、构造函数、继承类的执行顺序为: 


    1、父类的静态变量; 

    2、子类的静态变量; 

    3、父类的普通变量、父类的构造函数; 

    4、子类的普通变量、子类的构造函数; 

    5、普通方法。

  • 相关阅读:
    模拟112 题解
    python 虚拟环境--virtualenv
    Flask基础
    通过yum安装mysql数据
    Python中function(函数)和methon(方法)的区别
    Django之路由分发系统
    OpenGL Shader in OpenCASCADE
    A Simple OpenGL Shader Example II
    Make Helix Curve in OpenCASCADE
    A Simple OpenGL Shader Example
  • 原文地址:https://www.cnblogs.com/winkey4986/p/2343288.html
Copyright © 2011-2022 走看看