zoukankan      html  css  js  c++  java
  • java练习题3

    代码验证静态代码块被自动执行,就算产生了类的多个实例对象,但其中的静态代码块只被执行了一次。

    class Parent{
      static String name="hello";
       {
          System.out.println("parent block");
        }
      static {
          System.out.println("parent static block");
        }
      public Parent(){
          System.out.println("parent constructor");
        }
    }
    class Child extends Parent{
      static String childName="hello";
        {
           System.out.println("child block");
         }
      static {
          System.out.println("child static block");
        }
      public Child(){
          System.out.println("child constructor");
        }
    }
    public class TextStatic{
      public static void main(String[] args){
         new Child();
         new Child();
      }
    }

    运行结果:

     

  • 相关阅读:
    bzoj1711
    bzoj1458
    bzoj1433
    hdu2732
    bzoj1066
    hdu3549
    poj1698
    [ZJOI2007]时态同步
    SA 学习笔记
    [LUOGU]2016 Sam数
  • 原文地址:https://www.cnblogs.com/ljs-666/p/7747141.html
Copyright © 2011-2022 走看看