简体   繁体   English

如何在两个不同的类文件中的方法外部使用公共变量?

[英]How can I use a public variable outside a method in two different class files?

I have 2 Class files.我有 2 个类文件。 Class_login.java and Class_Company.java. Class_login.java 和 Class_Company.java。 I have an xpath stored in different location in a properties file.我有一个 xpath 存储在属性文件的不同位置。 A method is written in Class_login.java to load this properties file.在 Class_login.java 中编写了一个方法来加载此属性文件。

static Properties objprop1 = new Properties();
public static FileInputStream fileInputS = null;

static void propManager() throws IOException {
        fileInputS = new FileInputStream("C:\\Test-Automation\\FinanceSys\\myproj\\src\\test\\resources\\xpath.properties");
        objprop1.load(fileInputS);
}

objprop1 is declared outside the method in Class_login.java . objprop1Class_login.java的方法外声明。
I need to load this file again in Class_Company.java .我需要在Class_Company.java中再次加载此文件。 If I use it like Class_login.PropertyManager();如果我像Class_login.PropertyManager();这样使用它and use same objprop1 the file does not load and the xpaths are not found.并使用相同的 objprop1 文件不会加载并且找不到 xpaths。
Hence I created same method with a different name( static void PropertyManager() { ) and因此,我创建了具有不同名称的相同方法( static void PropertyManager() { )和
public static Properties objprop = new Properties();
I know this is not the correct way.我知道这不是正确的方法。 But how can this be done otherwise?但是,否则怎么办呢? In the main method of Class_Company.java I called these methods separately so that I do not get a null xpath error as earlier.Class_Company.java的主要方法中,我分别调用了这些方法,这样我就不会像之前那样得到 null xpath 错误。

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    Class_login LogFeature = new Login();
    Class_Company CC = new CCompany();
    **Class_Company.PropertyManager();**
    LogFeature.OpenBrowser("CH32");
    LogFeature.EnterURL("http://localhost:90/AppFin");
    LogFeature.PageMaximise();
    LogFeature.EnterUserName("uname");
    LogFeature.EnterPassword("abcd!@");
    LogFeature.ClickLoginButton();
    Thread.sleep(2000);
    **Class_Company.propManager();**
    CC.clickNewCompany("Manage");

Please tell me the right way of doing it.请告诉我正确的做法。 I want to use objprop in both the class files and only once in Class_Company.java.我想在两个类文件中都使用 objprop,并且只在 Class_Company.java 中使用一次。 and not twice as highlighted above.而不是上面强调的两倍。

This is the basic example, which you need:这是您需要的基本示例:

Global Variable declaration:全局变量声明:

Class variableCollection {
public static string data;
}

You can use this variables by extending class Or object references.您可以通过扩展类或对象引用来使用此变量。

class login extends variableCollection{


}

OR要么

class login{
variableCollection objvar = new variableCollection();

objvar.data = "";

}

You can use this same variable in multiple classes, same way.您可以在多个类中以相同的方式使用同一个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 firebase 实时数据库中检索两个不同的值并在 onDataChange 方法之外使用? - How can I retrieve two different values from firebase realtime database and use outside onDataChange method? 如何在其他类中使用方法? - How can I use a method in a different class? 如何在 onPostExecute 方法之外使用变量? - How can I use a variable outside the onPostExecute method? 我如何在包外访问默认类的公共方法 - how do i access a public method of a default class outside the package 我如何在这个公共空间之外使用变量 distanceHala? - How do I use the variable distanceHala outside of this public void? 如何使用来自不同类中的类的方法? - How can I use a Method from a class in a different class? 如何判断两个 class 文件为何/如何不同? - How can I tell why/how two class files are different? 如何在方法外部访问已在其他类中实例化的类A的成员变量? - How can I access the member variable of class A outside the method, that has been instantiated in other class? 如何在其他方法上使用变量集? - How can I use a variable set on a different method? 我可以在java中为公共变量和方法参数使用相同的名称吗? - Can I use same name for public variable and method argument in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM