Java 如何使用Builder模式链接UISelector对象的方法?
我正在尝试为Java 如何使用Builder模式链接UISelector对象的方法?,java,android,android-uiautomator,Java,Android,Android Uiautomator,我正在尝试为UISelector对象创建一个生成器类。以下是我的生成器类的外观: ## UISelectorBuilder.class package com.company.builders; import android.support.test.uiautomator.UiSelector; public class UISelectorBuilder { /** * Constants. */ private final UiSelector u
UISelector
对象创建一个生成器类。以下是我的生成器类的外观:
## UISelectorBuilder.class
package com.company.builders;
import android.support.test.uiautomator.UiSelector;
public class UISelectorBuilder
{
/**
* Constants.
*/
private final UiSelector uiSelector = new UiSelector();
/**
* Returns the {@link UiSelector}.
* @return {@link UiSelector}
*/
public UiSelector build()
{
return uiSelector;
}
/** ****** USER PROPERTIES *********
/**
* Method to generate a {@link UiSelector} based on the provided Class name.
* @param aClassName The Class name
* @return this
*/
public UISelectorBuilder addClassName(final String aClassName)
{
this.uiSelector.className(aClassName);
return this;
}
/**
* Method to generate a {@link UiSelector} based on the provided description.
* @param aDescription The description
* @return this
*/
public UISelectorBuilder addContainsDescription(final String aDescription)
{
this.uiSelector.descriptionContains(aDescription);
return this;
}
/**
* Method to generate a {@link UiSelector} based on the provided index.
* @param aIndex The index #
* @return this
*/
public UISelectorBuilder addIndexOf(final int aIndex)
{
this.uiSelector.index(aIndex);
return this;
}
/**
* Method to generate a {@link UiSelector} based on the provided resource ID.
* @param aResourceID The Resource ID
* @return this
*/
public UISelectorBuilder addResourceID(final String aResourceID)
{
this.uiSelector.resourceId(aResourceID);
return this;
}
/**
* Method to generate a {@link UiSelector} based on the provided text.
* @param aText The text to match
* @return this
*/
public UISelectorBuilder addText(final String aText)
{
this.uiSelector.text(aText);
return this;
}
/** ****** UI PROPERTIES *********
/**
* Method to generate a {@link UiSelector} based on whether the object is enabled or not.
* @param isEnabled True or False
* @return this
*/
public UISelectorBuilder addIsEnabled(final boolean isEnabled)
{
this.uiSelector.enabled(isEnabled);
return this;
}
/**
* Method to generate a {@link UiSelector} based on whether the object is selected or not.
* @param isSelected True or False
* @return this
*/
public UISelectorBuilder addIsSelected(final boolean isSelected)
{
this.uiSelector.selected(isSelected);
return this;
}
}
我希望能够通过只提供我想要的信息来动态创建UISelector
对象。例如:一个UISelector
可以只包含resourceID
,而另一个可以另外包含text
我使用生成器创建对象,如下所示:
new UISelectorBuilder().addResourceID(“resource”).build()代码>
问题是myUISelector
始终为空。我以前使用ArrayList
创建过构建器,没有遇到任何问题,但这个似乎不起作用。有人对我遗漏的东西有什么建议吗
问候,
Arjun如果您想要一些必填字段,请将它们设置为生成器构造函数的一部分问题是我的UISelector始终为空
,您能详细说明什么是空的吗?如果您想要一些必填字段,请将它们设置为生成器构造函数的一部分问题是我的UISelector始终为空
,什么是空的,你能详细说明一下吗?