如何根据条件创建一个全局变量

huangapple go评论63阅读模式
英文:

How to Create a Global Variable with a condition

问题

import java.lang.*;
import java.util.Properties;

public class constants {
    public static String driverFile = null;
    public static String storageDirectory = null;
    if (System.getProperty("os.name").startsWith("Windows")){
        storageDirectory = "C:\\Scripts\\Price_Tracker";
        driverFile = "geckodriver-v0.27.0-win64.exe";
    }else{//is linux
        storageDirectory = "/Price_Tracker";
        driverFile = "geckodriver-v0.27.0-linux64";
    }// end if(System.getProperty("os.name").startsWith("Windows"))

}//end public class constants
英文:

I want to create a global variable, but I want it to be one of two values, based on the machine's OS. I thought I could do this by writing the following code:

import java.lang.*;
import java.util.Properties;

public class constants {
    public static String driverFile = null;
    public static String storageDirectory = null;
        if (System.getProperty("os.name").startsWith("Windows")){
        storageDirectory = "C:\\Scripts\\Price_Tracker";
        driverFile = "geckodriver-v0.27.0-win64.exe";
    }else{//is linux
        storageDirectory = "/Price_Tracker";
        driverFile = "geckodriver-v0.27.0-linux64";
    }// end if(System.getProperty("os.name").startsWith("Windows")

}//end public class constants

but I get an error saying " Cannot resolve symbol 'getProperty' " , is what I'm trying to do even possible?

如何根据条件创建一个全局变量

答案1

得分: 1

public class constants {
    public static String driverFile = null;
    public static String storageDirectory = null;

    static {
        if (System.getProperty("os.name").startsWith("Windows")) {
            storageDirectory = "C:\\Scripts\\Price_Tracker";
            driverFile = "geckodriver-v0.27.0-win64.exe";
        } else { // is linux
            storageDirectory = "/Price_Tracker";
            driverFile = "geckodriver-v0.27.0-linux64";
        } // end if(System.getProperty("os.name").startsWith("Windows"))
    }

}// end public class constants
英文:
public class constants {
    public static String driverFile = null;
    public static String storageDirectory = null;

    static {
        if (System.getProperty("os.name").startsWith("Windows")){
            storageDirectory = "C:\\Scripts\\Price_Tracker";
            driverFile = "geckodriver-v0.27.0-win64.exe";
        }else{//is linux
            storageDirectory = "/Price_Tracker";
            driverFile = "geckodriver-v0.27.0-linux64";
        }// end if(System.getProperty("os.name").startsWith("Windows")
    }

}//end public class constants

huangapple
  • 本文由 发表于 2020年9月10日 06:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63820193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定