我正在使用 Selenium 及其 PageFactory 设计一个应用程序。
我的每个页面对象类都继承自具有标准方法的基类,但我也有一些页面需要 SlowLoadableComponent。由于这是作为抽象类实现的,所以我不能同时扩展它和我的基页类。
让我的基页类扩展 SlowLoadableComponent 有什么缺点吗?如果是这样,您会将基页面类实现为具有默认方法或其他方法的接口(interface)吗?
最佳答案
要是你有multiple inheritance就好了...
Java 中多重继承的解决方法是 delegation .
因此您可以将代码重构为如下所示:
public interface IBasePage {
//... unimplemented base page methods
}
public class BasePageImpl implements IBasePage {
public BasePageImpl(WebDriver webDriver) { //... }
//... implemented base page methods
}
public class FastPage implements IBasePage {
//... delegate all base page methods to the BasePageImpl
}
public class SlowPage extends SlowLoadableComponent implements IBasePage {
//... delegate all base page methods to the BasePageImpl
}
关于java - 使用 Java Selenium SlowLoadableComponent 的类结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920323/