类中的循环依赖关系和StackOverflow错误 [英] Circular Dependency in classes and StackOverflow Error

查看:47
本文介绍了类中的循环依赖关系和StackOverflow错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我一直遵循的做法可能是不合适的.还在这里寻找解决我的问题的方法:

Though the practice that I have been following could be inappropriate. Still looking for a fix to my problem here :

我遇到了StackOverflowError ::

I'm getting a StackOverflowError ::

java.lang.StackOverflowError
    at com.my.package.pages.Selendroid.HomePage.<init>(HomePage.java:11)
    at com.my.package.pages.Selendroid.LoginPage.<init>(LoginPage.java:14)
    at com.my.package.pages.Selendroid.HomePage.<init>(HomePage.java:11)

AppiumBasePage ::

AppiumBasePage ::

public class AppiumBasePage {
    protected AppiumDriver driver;

主页::

public class HomePage extends AppiumBasePage {
    LoginPage loginPage = new LoginPage();

LoginPage ::

LoginPage ::

public class LoginPage extends AppiumBasePage {
    HomePage homePage = new HomePage();

Q:我该如何解决这种循环依赖性,在这里我到底在做什么错?细节会很棒.

Q : How do I resolve this cyclic dependency and what exactly am I doing wrong here? Details would be great.

我要实现的详细信息是-在这两个类中我将有很多相互关联的方法.而且,我不想在要多次使用的函数内部创建对象,而是希望在一个页面上有一个对象,而在另一个页面上可以使用前一个对象中定义的方法.

Edit : The details to what I want to achieve is - I would be having a lot of interrelated methods in both these classes. And instead of creating an object inside the functions where I wanted to use it multiple times, I want to have a single object of one page on another to use the methods defined in the former.

推荐答案

您的问题是,当您创建 HomePage 时,它会创建一个新的 LoginPage ,并且每当您创建一个 LoginPage LoginPage 创建一个 Home .显然,这将导致永不结束(直到堆栈溢出)循环.

Your problem is that when you create a HomePage it creates a new LoginPage and whenever you create a LoginPage you create a HomePage. This will clearly result in a never ending (until the stack overflows) cycle.

要解决此问题,请不要在构建过程中创建页面.为他们设置 setter s

To solve the problem, do not create the pages during construction. Make setters for them

private static class AppiumBasePage {

    public AppiumBasePage() {
    }
}

public class HomePage extends AppiumBasePage {

    LoginPage loginPage;

    public void setLoginPage(LoginPage loginPage) {
        this.loginPage = loginPage;
    }

}

public class LoginPage extends AppiumBasePage {

    HomePage homePage;

    public void setHomePage(HomePage homePage) {
        this.homePage = homePage;
    }


}

public void test() {
    LoginPage login = new LoginPage();
    HomePage home = new HomePage();
    login.setHomePage(home);
    home.setLoginPage(login);
}

或者,您可以通过引入一个新类来完全消除相互依赖性.

Alternatively you could remove the interdependency entirely by introducing a new class to maintain that.

public class HomePage extends AppiumBasePage {

}

public class LoginPage extends AppiumBasePage {
}

class Pages {

    AppiumBasePage login = new LoginPage();
    AppiumBasePage home = new HomePage();
}

这完全取决于您需要实现的目标.

It all depends on what you need to achieve.

这篇关于类中的循环依赖关系和StackOverflow错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆