[英]Java- use a variable from another file
How can I use a variable from another file?如何使用另一个文件中的变量? I am writing automation tests (Selenium and TestNG).
我正在编写自动化测试(Selenium 和 TestNG)。 I want to store some data variables and xpaths in a separate file (secondFile.java)
我想将一些数据变量和 xpath 存储在一个单独的文件中(secondFile.java)
Master File:主文件:
import secondFile.help;
public class TicketAdminTestSuite extends something {
void printStuff(){
System.out.println(bar);
}
}
============================================== ===============================================
Helper file (name: help.java):帮助文件(名称:help.java):
public class help {
public static final String bar = "blah";
}
There are two severe errors in your code:您的代码中有两个严重错误:
- the helper file name must be the same as the class name - 帮助文件名必须与类名相同
- the import in the master file must import the helper file with the full package name (I assume the files are in the same package) - 主文件中的导入必须以完整的包名导入帮助文件(我假设文件在同一个包中)
// master file TicketAdminTestSuite.java
import Help;
public class TicketAdminTestSuite extends something {
void printStuff(){
System.out.println(Help.bar);
}
}
// help file Help.java
public class Help {
public static final String bar = "blah";
}
Just write help.bar:只需编写 help.bar:
void printStuff(){
System.out.println(help.bar);
But this example is a bit confusing because the public class must be called the same as the .java file.但是这个例子有点混乱,因为公共类必须和 .java 文件一样被调用。 And if you made a second class in the secoundfile you wouldn't be able to access it from your first file.
而且,如果您在 secoundfile 中创建了第二个类,您将无法从第一个文件中访问它。 This would be a better example: import secondFile;
这将是一个更好的例子: import secondFile;
public class TicketAdminTestSuite extends something {
void printStuff(){
System.out.println(secondFile.BAR);
}
}
And the second file is made like this第二个文件是这样制作的
public class secondFile {
public static final String BAR = "blah";
}
Simpliest way to do this is to import your help
class and access it like:最简单的方法是导入您的
help
类并像这样访问它:
import help;
...
String test = help.bar;
... or you can use static import : ...或者您可以使用静态导入:
import static help.bar;
...
String test = bar;
public class
name should match with its file name. public class
名应与其文件名匹配。
eg:例如:
First.java第一个.java
import demo.Second;
public class First{
Second second=new Second();
void printStuff(){
System.out.println(second.getBar());
}
}
Second.java二.java
package demo;
public class Second{
String bar="blah";
public String getBar(){
return this.bar;
}
}
it was excruciating, but I managed to do it.这很痛苦,但我设法做到了。 my code for the first file, "Main.java" is:
我的第一个文件“Main.java”的代码是:
import maintu.*;
public class Main extends maintu.Maintu {
public static void main(String[] args) {
System.out.println("println");
System.out.println("I am on a different line");
System.out.print("I am print ");
System.out.print("I will be on the same line ");
System.out.println(3 + 3);
// I will be ignored
System.out.println("there is a comment after me"); // so will I
//System.out.println("I will be ignored too");
/* I will be ignored until I end
not over yet.
Now i am over*/
System.out.println("I will print though");
String variable = "you can use me to call on me for later ";
System.out.println(variable + "and I dont need quotes!");
int numVariable = 13;
System.out.print("my number is " + numVariable + ".");
String hi;
hi = "This works too!";
System.out.println(hi);
int x;
x = 10;
x = 20;
System.out.println(x);
final String finel = "I cannot change";
final int que; // I will always be empty. not 0, empty
int w = 5, y = 6, z = 50; /*you can set several variables in one line like this*/
/*you can do this as well :
int x, y, z;
x = y = z = 50;
*/
int wholenumber = 1;
float onepointone = 5.99f;
char onesinglecharacter = 'D';
boolean truefalse = true;
String unlimitedwords = "string = words";
byte onezero = -13;
short hexagecimal = -13000;
long isverylong = -4775808;
double bdoublo;
x=0;
System.out.println(maintu.Maintu.maybe);/*
while(true) {
x = x + 1;
System.out.print(x + " ");
}*/
}
}
and my code for the, as you call it "helper" file, "maintu.java" is:我的代码,正如你所说的“帮助”文件,“maintu.java”是:
package maintu;
public class Maintu {
public static String maybe = "extends";
}
the way I did it was: write code for files in command prompt: run javac Maintu.java
in command prompt: run javac Maintu.java
in command prompt: run javac Main.java
in command prompt: run java Main.java
command prompt output:我这样做的方法是:在命令提示符下为文件编写代码:在命令提示符下运行
javac Maintu.java
:在命令提示符下运行javac Maintu.java
:在命令提示符下运行javac Main.java
:运行java Main.java
命令提示符输出:
println
I am on a different line
I am print I will be on the same line 6
there is a comment after me
I will print though
you can use me to call on me for later and I dont need quotes!
my number is 13.This works too!
20
extends
I am using java 17我正在使用 java 17
Use a getter method.使用 getter 方法。
public class help {
public static final String bar = "blah";
public String getBar(){
return this.bar;
}
}
And then use it on your code.然后在你的代码上使用它。
import secondFile.help;
public class TicketAdminTestSuite extends something {
Help help = new Help();
void printStuff(){
System.out.println(help.getBar());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.