簡體   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