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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 一帆风顺 中级黑马   /  2012-10-5 22:37  /  1693 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王博 于 2012-10-5 22:43 编辑

//对象初始化顺序

public class A extends Letter
{
       //非静态成员变量
              int a1=10;
              int a2=am();
      //静态成员变量
              static int sa1=20;
              static int sa2=sam();
      //非静态代码块
        {
              System.out.println("A's instance initialize block");
        }
      //静态代码块
        static{
              System.out.println("A's static initialize block");
        }
      //非静态方法
        int am()
       {
              System.out.println("A's instance method");
              return 21;
       }
     //静态方法
       static int sam()
      {
             System.out.println("A's static method");
             return 11;
       }
     //构造方法
       public A(String s)
      {
              super(s);
              System.out.println("A's constructor");
       }
      //main方法
        public static void main(String[] args)
        {
              new A("class A'constructor paramter");
        }
}
class Letter
{
       //非静态成员变量
              int l1=30;
              int l2=lm();
       //静态成员变量
              static int sl1=40;
              static int sl2=slm();
       //非静态代码块
         {
               System.out.println("Letter's instance initialize block");
         }
       //静态代码块
         static{
               System.out.println("Letter's static initialize block");
         }
       //非静态方法
         int lm()
        {
             System.out.println("Letter's instance method");
             return 31;
        }
      //静态方法
        static int slm()
        {
              System.out.println("Letter's static method");
              return 41;
         }
      //构造方法
        public Letter(String s)
        {
               System.out.println("Letter's constructor");
               System.out.println("The parameter\" "+s+"\" from A has reach ");
        }
}


输出结果















评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

2 个回复

倒序浏览
Person p = new Person("lisi",30);
该句话都做了什么事情?
1、因为new用到了Person.class所以会先找Person.class文件并卡加载到内存中。

2、执行该类中的static代码块,如果有的话。给Person.class类进行初始化。


3、在堆内存中开辟空间分配内存地址。


4、在堆内存中建立对象的特有属性。并进行默认初始化。


5、对属性进行显示初始化。


6、对对象进行构造代码块初始化。


7、对对象进行对应的构造函数初始化。


8、将内存地址赋给栈内存中的p。

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 25了

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马