如何在内部类中调用继承的类构造函数

时间:2017-08-16 10:55:24

标签: java inner-classes

我需要在内部类中调用继承的类构造函数。假设我有继承Baseclass的Outerclass,我在Outerclass中有一个内部类。从这个内部类的构造函数我需要调用BaseClass构造函数吗?

public class BaseClass {
    public BaseClass (WebDriver driver)
    {
        this.driver =driver;
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, maxTimeOutForElement), this);    
    }
}

public class OuterClass extends BaseClass {
    OuterClass (WebDriver driver)
    {
        super(driver)
    }

    //Does something

    public class InnerClass
    {
         InnerClass (WebDriver driver)
         {
             //is there a way  to call Baseclass constructor super(driver) 
             //I don't want to call directly new AuthorLoginPage(driver) breaking
             //my constructor chain.
         }
    }
}

2 个答案:

答案 0 :(得分:2)

答案:

//is there a way  to call Baseclass constructor super(driver) 

。请注意:InnerClass不是静态。这意味着:InnerClass的对象不能“自己”存在。创建InnerClass实例的唯一方法是第一次创建OuterClass的实例。

在可以创建InnerClass个对象(正在执行它们的构造函数代码)的时间点 - 调用超类的构造函数没有意义。因为InnerClass没有超类(除了Object)。调用封闭OuterClass的构造函数是没有意义的 - 因为OuterClass对象已经存在!

或者,通过代码解释:

OuterClass outer = new OuterClass(someDriver);
OuterClass.InnerClass inner = outer.new InnerClass(someDriver); 

导致:退后一步,看看你的模特。在第一次更深入的一瞥中没有多大意义。

要解决OP的评论:InnerClass 扩展对象!它与BaseClass 直接关系。除了任何InnerClass对象属于OuterClass对象的事实 - 并且该对象扩展为BaseClass

答案 1 :(得分:1)

从BaseClass扩展InnerClass,你会很好。以下是代码的略微修改版本:

    public static class BaseClass {
        public BaseClass (String driver)
        {
            System.out.println(getClass().getSimpleName() + ": " + driver);
        }

        public class OuterClass extends BaseClass {
            OuterClass (String driver)
            {
                super(driver);
            }
            //Does something

            public class InnerClass extends BaseClass
            {
                InnerClass (String driver)
                {
                    super(driver);
                    //is there a way  to call Baseclass constructor super(driver)
                    //I don't want to call directly new AuthorLoginPage(driver) breaking
                    //my constructor chain.
                }
            }

        }

        public static void main(String[] args) {
            BaseClass clz = new BaseClass("one").new OuterClass("two").new InnerClass("three");
            // prints
            // BaseClass: one
            // OuterClass: two
            // InnerClass: three
        }
    }

如果您不想从BaseClass扩展InnerClass,那么将初始化逻辑移动到init(String driver)方法并通过BaseClass.this.init(驱动程序)调用它:

public static class BaseClass {
    public BaseClass (String driver)
    {
        init(driver);
    }


    public void init(String driver){
        System.out.println(getClass().getSimpleName() + ".init: " + driver);

    }

    public class OuterClass extends BaseClass {
        OuterClass (String driver)
        {
            super(driver);
        }
        //Does something

        public class InnerClass
        {
            InnerClass (String driver)
            {
                BaseClass.this.init(driver);
            }
        }

    }
}