从主要方法
问题描述:
调用变量我有基于以下代码的问题:从主要方法
public class LoginCaptchaChrome {
public static void main(String[] args) throws IOException, InterruptedException{
String tc = args[0];
String address = args[1];
String test_data = args[2];
String test_result = args[3];
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lam Chio Meng\\Desktop\\work\\chromedriver_win32\\chromedriver.exe");
//Do other stuff
}
//runTest is called from a different class
public static void runTest(String string0, String string, String string1) throws InterruptedException, IOException{
WebDriver login = new ChromeDriver();
System.out.println(login);
login.get(address);
//Do other things
}
}
我从通过命令提示符下执行过程中传递的参数获得值tc,address,test_data
和test_result
。现在,我想将address
的值传递给位于runTest方法中的login.get(address)
。
我现在无法这样做,因为我知道要发生这种情况,变量address
必须在主方法之外声明。我不能在主方法外部声明address
变量,因为它从命令提示符处接收参数。请记住runTest方法已被分配为接受来自不同类的另一个方法的值。希望能向大家提供如何在runTest方法中将主要方法中的address
值传递给address
变量的建议。
答
您可以在class LoginCaptchaChrome
中声明一个字段。声明一个可以在主要方法中访问的静态字段。
记住非静态字段需要一个要访问的类(一个对象)的实例,静态字段不需要实例并且可以通过静态上下文访问。
现在一些代码示例:
public class LoginCaptchaChrome {
public static Optional<String> addressOptional = Optional.empty
public static void main(String[] args) throws IOException, InterruptedException{
String tc = args[0];
String address = args[1];
String test_data = args[2];
String test_result = args[3];
//using Optional since it will simply handling value not present cases
addressOptional = Optional.of(args[4]);
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lam Chio Meng\\Desktop\\work\\chromedriver_win32\\chromedriver.exe");
//Do other stuff
}
//runTest is called from a different class
public static void runTest(String string0, String string, String string1) throws InterruptedException, IOException{
WebDriver login = new ChromeDriver();
System.out.println(login);
//Using this you throw an error when no address is supplied via cmd ,
//if address is supplied then you will get it here
login.get(addressOptional.orElseThrow(()->new NoSuchElement("address is not provided")))
//Do other things
}
}
阅读有关使用Java 8可选,如果你不熟悉,它可以帮助你避免空,采取更加实用的方法。
答
为了保持简单:LoginCaptchaChrome
创建静态字段:
public class LoginCaptchaChrome {
static String address = ""; // <-- private if only set from main, otherwise public
public static void main(String[] args) throws IOException, InterruptedException{
String tc = args[0];
address = args[1];
String test_data = args[2];
String test_result = args[3];
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lam Chio Meng\\Desktop\\work\\chromedriver_win32\\chromedriver.exe");
//Do other stuff
}
//runTest is called from a different class
public static void runTest(String string0, String string, String string1) throws InterruptedException, IOException{
WebDriver login = new ChromeDriver();
System.out.println(login);
login.get(address);
//Do other things
}
}
的另一种选择是在main()
System.setProperty("LoginCaptchaChrome.address", address);
或命令的使用系统属性行:
-DLoginCaptchaChrome.address=<address>`
和在登录用它
login.get(System.getProperty("LoginCaptchaChrome.address"));
HTH
“我不能声明地址变量的主要方法外,因为它是从命令提示接收arguements。”, - 如何分配'main'方法中的变量限制它不被声明为字段? – user3707125
嗨用户,感谢您的反馈。我相信这是因为如果我在主方法之外声明它们,我将无法接受从命令提示符执行期间传递的争论。这有助于澄清?如果我在这方面有所欠缺,我很抱歉。基本上,我会将这段代码导出到jar文件中,并且将通过java -jar c:\ Test \ LoginCaptchaChrome test1 www.test.com c:\ testdata \ data.xlsx c:\ testresult \ result.xlsx – lamCM
“如果我在主要方法之外声明它们,我将无法接受从命令提示符执行期间传递的争论。“ - 为什么? – user3707125