zoukankan      html  css  js  c++  java
  • 8.3-构造器调用顺序

    8.3.1 构造器的调用顺序
    让我们来看下面这个例子,它展示组合、继承以及多态在构建顺序上作用:
    //: polymorphism/Sandwich..java
    // Order of constructor calls.
    package polymorphism;

    class Meal {
        Meal() {System.out.println("Meal()");}
    }
    class Bread {
        Bread() {System.out.println("Bread()");}
    }
    class Cheese {
        Cheese() {System.out.println("Cheese()");}
    }
    class Lettuce {
        Lettuce() {System.out.println("Lettuce()");}
    }
    class Lunch extends Lettuce {
        Lunch() {System.out.println("Lunch()");}
    }
    class PortableLunch extends Lunch{
        PortableLunch() {System.out.println("PortableLunch()");}
    }
    public class Sandwich extends PortableLunch{
        private Bread b = new Bread();
        private Cheese c = new Cheese();
        private Lettuce l = new Lettuce();
        public Sandwich(){
            System.out.println("Sandwich()");
        }
        public static void main(String[] args) {
            new Sandwich();
        }
    }
    /* Output
    Lettuce()
    Lunch()
    PortableLunch()
    Bread()
    Cheese()
    Lettuce()
    Sandwich()
     *///~
    

    在这个例子中,用其他类创建了一个复杂的类,而且每个类都有一个声明它自己的构造器。其中最重要的类是Sandwich,它反映了三层继承(若将Object的隐含继承也算在内,就是四层)以及三个成员对象。当在main()里创建一个Sandwich对象后,就可以看到输出结果。这也表明了这一复杂对象调用构造器要遵照下面的顺序:

    1. 调用基类构造器。这个步骤会不断反复递归下去,首先是构造这种层次结构的根,然后是下一层导出类,等等,直到最底层的导出类。
    2. 按声明顺序调用成员的初始化方法。
    3. 调用导出类构造器的主体。

    构造器的调用顺序是很重要的。当进行继承时,我们已经直到基类的一切,并且可以访问基类中任何声明为public和protected的成员。这意味着在导出类中,必须假定基类的所有成员都是有效的。一种标准方法是,构造器动作一经发生,那么对象所有部分的全体成员都会得到构建。然而,在构造器内部,我们必须确保索要使用的成员都已经构建完毕。为确保这一目的,唯一的方法就是首先调用基类构造器。那么在进入导出类构造器汇时,在积累中可供我们访问的成员都已得到初始化。此外,知道构造器中的所有成员都有效也是因为,当成员对象在类内进行定义的时候(比如上栗中的b、c和l),只要有可能,就应该对他们进行初始化(也就是说,通过组合方法将对象置于类内)。若遵循这一规则,那么就能确保所有基类成员以及当前对象的成员对象都被初始化了。但遗憾的是,这种做法并不适用于所有情况,这一点会在本篇末尾看到。

    8.3.2 继承与清理
    通过组合和继承方法来创建新类时,永远不必担心对象的清理问题,子对象通常都会留给垃圾回收器进行处理。如果确实遇到清理的问题,那么必须用心为新类创建dispose()方法(起的名字)。并且由于继承的缘故,如果我们有其他作为垃圾回收一部分的特殊清理动作,就必须在导出类中覆盖dispose()方法。当覆盖被继承类的dispose()方法时,务必记住调用基类版本dispose()方法;否则,基类的清理动作就不会发生。下例证明这一点:
    //:polymorphism/Frog.java
    // Cleanup and inheritance
    package polymorphism;

    class Characteristic {
        private String s;
        Characteristic(String s) {
            this.s = s;
            System.out.println("Creating Characteristic " + s);
        }
        protected void dispose() {
            System.out.println("disposing Characteristic " + s);
        }
    }
    class Description {
        private String s;
        Description(String s) {
            this.s = s;
            System.out.println("Creating Description " + s);
        }
        protected void dispose() {
            System.out.println("disposing Description " + s);
        }
    }
    class LivingCreature {
        private Characteristic p = new Characteristic("is alive");
        private Description t = new Description("Basic Living Creature");
        LivingCreature() {
            System.out.println("LivingCreature()");
        }
        protected void dispose() {
            System.out.println("LivingCreature dispose");
            t.dispose();
            p.dispose();
        }
    }
    class Animal extends LivingCreature {
        private Characteristic p = new Characteristic("has heart");
        private Description t = new Description("Animal not Vegetable");
        Animal(){System.out.println("Animal()");}
        protected void dispose() {
            System.out.println("Animal dispose");
            t.dispose();
            p.dispose();
            super.dispose();
        }
    }
    class Amphibian extends Animal {
        private Characteristic p = new Characteristic("can live in water");
        private Description t = new Description("Both water and land");
        Amphibian() {
            System.out.println(" Amphibian()");
        }
        protected void dispose() {
            System.out.println("Amphibian dispose");
            t.dispose();
            p.dispose();
            super.dispose();
        }
    }
    public class Frog extends Amphibian {
        private Characteristic p = new Characteristic("Croaks");
        private Description t = new Description("Eats Bugs");
        public Frog() {System.out.println("Frog()");}
        protected void dispose() {
            System.out.println("Frog dispose");
            t.dispose();
            p.dispose();
            super.dispose();
        }
        public static void main(String[] args) {
            Frog frog = new Frog();
            System.out.println("Bye!");
            frog.dispose();
        }
    }
    /*Output
    Creating Characteristic is alive
    Creating Description Basic Living Creature
    LivingCreature()
    Creating Characteristic has heart
    Creating Description Animal not Vegetable
    Animal()
    Creating Characteristic can live in water
    Creating Description Both water and land
     Amphibian()
    Creating Characteristic Croaks
    Creating Description Eats Bugs
    Frog()
    Bye!
    Frog dispose
    disposing Description Eats Bugs
    disposing Characteristic Croaks
    Amphibian dispose
    disposing Description Both water and land
    disposing Characteristic can live in water
    Animal dispose
    disposing Description Animal not Vegetable
    disposing Characteristic has heart
    LivingCreature dispose
    disposing Description Basic Living Creature
    disposing Characteristic is alive
     */
    

    层次结构中的每个类都包含Characteristic和Description这两种类型的成员对象,并且它们也必须被销毁。所以外衣某个子对象要依赖其他对象,销毁的顺序应该和初始化顺序相反。对于字段,则意味着与生命的顺序相反(因为字段的初始化是按照声明的顺序进行的)。对于基类(遵循C++中析构函数的形式),应该首先对其导出类进行清理,然后才是基类。这是因为导出类的清理可能会调用基类的某些方法,所以需要使基类中的构件仍起作用而不应过早地销毁它们。从输出结果看到,Frog对象的所有部分都是按照创建的逆序进行销毁的。

    在这个例子中可以看到,尽管通常不必执行清理工作的,但是一旦选择要执行,就必须谨慎和小心。

    8.3.3 构造器内部的多态方法的行为
    构造器调用的层次结构带来了一个有趣的两难问题。如果在一个构造器的内部调用正在构造的对象的某个动态绑定的方法,那会发生什么情况呢?

    在一般的方法内部,动态绑定的调用是在运行时才决定的,因为对象无法知道它是属于方法所在的那个类,还是属于那个类的导出类。

    如果要调用构造器内部的一个动态绑定方法,就要用哪个方法的被覆盖后的定义。然而,这个调用的效果可能相当难于预料,因为被覆盖的方法在对象被完全构造之前就会被调用。这可能会造成一些难于发现的隐藏错误。

    从概念上将,构造器的工作实际上是创建对象(这并非是一件平常的工作)。在任何构造器内部,整个对象可能只是部分形成-我们只知道基类对象已经进行初始化。如果构造器只是在构建对象过程中的一个步骤,并且该对象所属的类是从这个构造器所属的类导出的,那么导出部分在当前构造器正在被调用的时刻仍旧是没有被初始化的。然而,一个动态绑定的方法调用缺会向外深入到继承层次结构内部,它可以调用导出类里的方法。如果我们是在构造器内部这样做,那么就可能会调用某个方法,而这个方法所操纵的成员可能还未进行初始化-这肯定会招致灾难。

    通过下面这个例子,我们会看到问题所在:
    //: polymorpism/PolyConstructors.java
    // Constructors and polymorphism
    // don`t product what you might expect.
    package polymorphism;

    class Glyph {
        void draw(){System.out.println("Glyph.draw()");}
        Glyph() {
            System.out.println("Glyph() before draw()");
            draw();
            System.out.println("Glyph after draw()");
        }
    }
    class RoundGlyph extends Glyph {
        private int radius = 1;
        RoundGlyph(int r) {
            radius = r;
            System.out.println("RoundGlyph.RoundGlyph().radius = " + radius);
        }
    }
    public class PolyConstructors {
        public static void main(String[] args) {
            new RoundGlyph(5);
        }
    }
    /*Output
    Glyph() before draw()
    Glyph.draw()
    Glyph after draw()
    RoundGlyph.RoundGlyph().radius = 5
     */
    

    Glyph.draw()方法设计为将要被覆盖,这种覆盖是在RoundGlyph中发生的。但是Glyph构造器汇调用这个方法,结果到会了对RoundGlyph.draw()条用,这看起来似乎是我们的目的。但是如果看到输出结果,我们会发现当Glyph的构造器调用draw()方法时,radius不是默认初始值1,而是0.这可能导致在屏幕上只画了一个点,或者根本什么东西都没有;我们只能干瞪眼,并试图找出程序无法运转的原因所在。

    文章开头讲述的初始化顺序并不是十分完整,而这正式解决这谜题的关键所在。初始化的实际过程是:

    1. 在其他任何事物发生之前,将分配给对象的存储空间初始化成二进制的聆。
    2. 如果前述那样调用基类构造器。此时,调用被覆盖后的draw()方法(要在调用RoundGlyph构造器之前调用),由于步骤1的缘故,我们此时会发现radius的值为0.
    3. 按照声明的顺序调用成员的初始化方法。
    4. 调用导出类的构造器主体。

    这样做有一个有点,那就是所有东西都至少初始化为零(或者某些特殊数据类型中与“零”等价值的值),而不是仅仅留作垃圾。

    因此,编写构造器时有一条有效的准侧:“用尽可能简单的那些方法使对象进入正常状态;如果可以的话,避免调用其他方法”。在构造器内唯一能够安全调用的那些方法是基类中的final方法(也适用于private方法,他们自动属于fianl方法)。这些方法不能被覆盖,因此也就不会出现上述令人惊讶的问题。你可能无法总是能够遵循这条准则,但是应该朝着他努力。

  • 相关阅读:
    java, listmap2json, fastjson
    java, mybatis, 调用mysql存储过程
    解析json串,利用正则表达式,split
    alibaba fastjson List<Map<String, String>>2Str
    getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:
    message from server: "Host 'XXX' is not allowed to connect to this MySQL server
    Java之工厂方法
    oracle 的分析函数
    在js中怎样获得checkbox里选中的多个值?(jQuery)
    获得select被选中option的value和text
  • 原文地址:https://www.cnblogs.com/cgy-home/p/11130246.html
Copyright © 2011-2022 走看看