繁体   English   中英

Java-使用另一个文件中的变量

[英]Java- use a variable from another file

如何使用另一个文件中的变量? 我正在编写自动化测试(Selenium 和 TestNG)。 我想将一些数据变量和 xpath 存储在一个单独的文件中(secondFile.java)

主文件:

import secondFile.help;

public class TicketAdminTestSuite extends something {

       void printStuff(){
            System.out.println(bar);
} 

}

===============================================

帮助文件(名称:help.java):

public class help  {
    public static final String bar = "blah";
}

您的代码中有两个严重错误:
- 帮助文件名必须与类名相同
- 主文件中的导入必须以完整的包名导入帮助文件(我假设文件在同一个包中)

// 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";
}

只需编写 help.bar:

   void printStuff(){
        System.out.println(help.bar);

但是这个例子有点混乱,因为公共类必须和 .java 文件一样被调用。 而且,如果您在 secoundfile 中创建了第二个类,您将无法从第一个文件中访问它。 这将是一个更好的例子: import secondFile;

public class TicketAdminTestSuite extends something {

   void printStuff(){
        System.out.println(secondFile.BAR);
} 

}

第二个文件是这样制作的

public class secondFile  {
     public static final String BAR = "blah";
}

最简单的方法是导入您的help类并像这样访问它:

import help;

...

String test = help.bar;

...或者您可以使用静态导入

import static help.bar;

...

String test = bar;

public class名应与其文件名匹配。

例如:

第一个.java

import demo.Second;
public class First{
 Second second=new Second();
       void printStuff(){
            System.out.println(second.getBar());
       } 
}

二.java

 package demo;
    public class Second{
     String bar="blah";
     public String getBar(){
              return this.bar;
     }
    }

这很痛苦,但我设法做到了。 我的第一个文件“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 + " ");
        }*/
    }
}

我的代码,正如你所说的“帮助”文件,“maintu.java”是:

package maintu;
    public class Maintu {
        public static String maybe = "extends";
    }

我这样做的方法是:在命令提示符下为文件编写代码:在命令提示符下运行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

我正在使用 java 17

使用 getter 方法。

  public class help  {
        public static final String bar = "blah";            
        public String getBar(){
              return this.bar;
        }         
    }

然后在你的代码上使用它。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM