A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© as604049322 金牌黑马   /  2014-12-10 12:29  /  617 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 as604049322 于 2014-12-10 12:31 编辑

对象构造过程构造子类之前必须调用父类,且构造块会构造函数之前执行

  1. public class A {  

  2.         static{  
  3.                 System.out.println("A static block");  
  4.       }  
  5.          
  6.       public A() {  
  7.               super();  
  8.                 System.out.println("A constructor");  
  9.         }  
  10.         
  11.       {  
  12.               System.out.println("A not static block"+this);  
  13.       }  
  14. }
复制代码


B类:
  1. public class B extends A{  
  2.       static{  
  3.               System.out.println("B static block");  
  4.       }  
  5.         
  6.       public B() {  
  7.                 super();  
  8.               System.out.println("B constructor");  
  9.       }  
  10.         
  11.       {  
  12.               System.out.println("B not static block");  
  13.         }  
  14.         
  15.         public static void main(String[] args) {  
  16.                 new B();  
  17.         }  
  18. }
复制代码


结果:
  1. [*]A static block  
  2. [*]B static block  
  3. [*]A not static blockB@a90653  
  4. [*]A constructor  
  5. [*]B not static block  
  6. [*]B constructor  

复制代码



评分

参与人数 1技术分 +1 收起 理由
船长 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马