黑马程序员技术交流社区

标题: 一个类的执行过程,同大家分享 [打印本页]

作者: 金超    时间: 2012-4-28 22:12
标题: 一个类的执行过程,同大家分享
下面是代码摘字《Thinking In Java》,如果我没看过这本书,我也不懂。

问题:1、你在不用工具运行下面的代码,你可以推测结果么?请如实回答。
      2、 用工具运行得出结果,能根据结果解说缘由吗?

这是一个问题,也是一个调查,请如实回答,请勿搜索百度,那不是你我需要的答案。
只有清晰的知道自己的短处,才能补短,才知道自己应该着力的地方。[code=java]package day10.one;

class Bowl {
        Bowl(int marker) {
                System.out.println("Bowl(" + marker + ")");
        }

        void f(int marker) {
                System.out.println("f(" + marker + ")");
        }
}

class Table {
        static Bowl b1 = new Bowl(1);

        Table() {
                System.out.println("Table()");
                b2.f(1);
        }

        void f2(int marker) {
                System.out.println("f2(" + marker + ")");
        }

        static Bowl b2 = new Bowl(2);
}

class Cupboard {
        Bowl b3 = new Bowl(3);
        static Bowl b4 = new Bowl(4);

        Cupboard() {
                System.out.println("Cupboard()");
                b4.f(2);
        }

        void f3(int marker) {
                System.out.println("f3(" + marker + ")");
        }

        static Bowl b5 = new Bowl(5);
}

public class StaticInitialization {
        public static void main(String[] args) {
                System.out.println("Creating new Cupboard() in main");
                new Cupboard();
                System.out.println("Creating new Cupboard() in main");
                new Cupboard();
                t2.f2(1);
                t3.f3(1);
        }

        static Table t2 = new Table();
        static Cupboard t3 = new Cupboard();
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2