Java 中的静态字符串路径
Static String path in Java
我的小程序中只有一个 main class,而且它使用了很多路径。
因为当程序是 运行 时它们不会改变,我想我可以把 static
放在它们的声明中但不确定最终的。此外,我不确定在哪里是声明我的路径的最佳位置。它是在主要 class 中还是之前?
这是我的代码示例:
package classtest;
public class ClassTest {
// Should I put the path here?
public static void main(String[] args) {
String dirPath = "C:/Users/test1/";
String pathOut = "C:/Users/stats.csv";
// or here?
}
}
你可以做这种重构:
public class ClassTest {
// Specify a base path for all paths to be used
public static final String BASE_PATH = "C:/Users";
// 1. If these paths will be used in several methods, declare them here
public static final String dirPath = BASE_PATH + "/test1/";
public static final String pathOut = BASE_PATH + "/stats.csv";
public static void main(String[] args) {
// 2. If those paths will be used in the main method only, declare them here
final String dirPath = BASE_PATH + "/test1/";
final String pathOut = BASE_PATH + "/stats.csv";
}
}
class 的静态 成员应该在任何class 方法的范围 之外声明。
另一种方法是从 Properties 文件中读取路径:
Properties prop = new Properties();
并随心所欲地使用属性。它使以后的重构变得非常容易:
prop.getProperty("diPath");
prop.getProperty("pathOut");
更常见的是让你的路径参数,所以他们可以由 运行 程序的人设置。
public class ClassTest {
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("Usage: java ClassTest {dir} {output}");
return;
}
String dirPath = args[0];
String pathOut = args[1];
}
}
final
关键字意味着该值将 永远不会重新分配 。
static
关键字让变量成为class变量而不是实例变量。
补充说明,一般class常量都是大写加下划线分隔,所以改了名字。
所以如果你想声明它们"globally"最好是使用类似于下面的代码。
public class ClassTest {
public static final String DIR_PATH = "C:/Users/test1/";
public static final String PATH_OUT = "C:/Users/stats.csv";
public static void main(String[] args) {
// Use DIR_PATH or PATH_OUT as needed
}
}
请注意,只有在不同方法中重复使用 DIR_PATH
或 PATH_OUT
变量时,前面的代码才有用。否则,将变量定义为 main 方法的局部变量是正确的,以将可见性限制为仅使用它的代码部分。
试试这个..这是最干净的方法:
public class ClassTest implements StaticPath {
public static void main(String[] args) {
System.out.print(PATH_OUT);
}
}
interface StaticPath {
public final static String PATH = "C:/Users/";
public final static String PATH_OUT = PATH + "stats.csv";
public final static String PATH_IN = PATH + "dyn.csv";
}
我的小程序中只有一个 main class,而且它使用了很多路径。
因为当程序是 运行 时它们不会改变,我想我可以把 static
放在它们的声明中但不确定最终的。此外,我不确定在哪里是声明我的路径的最佳位置。它是在主要 class 中还是之前?
这是我的代码示例:
package classtest;
public class ClassTest {
// Should I put the path here?
public static void main(String[] args) {
String dirPath = "C:/Users/test1/";
String pathOut = "C:/Users/stats.csv";
// or here?
}
}
你可以做这种重构:
public class ClassTest {
// Specify a base path for all paths to be used
public static final String BASE_PATH = "C:/Users";
// 1. If these paths will be used in several methods, declare them here
public static final String dirPath = BASE_PATH + "/test1/";
public static final String pathOut = BASE_PATH + "/stats.csv";
public static void main(String[] args) {
// 2. If those paths will be used in the main method only, declare them here
final String dirPath = BASE_PATH + "/test1/";
final String pathOut = BASE_PATH + "/stats.csv";
}
}
class 的静态 成员应该在任何class 方法的范围 之外声明。
另一种方法是从 Properties 文件中读取路径:
Properties prop = new Properties();
并随心所欲地使用属性。它使以后的重构变得非常容易:
prop.getProperty("diPath");
prop.getProperty("pathOut");
更常见的是让你的路径参数,所以他们可以由 运行 程序的人设置。
public class ClassTest {
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("Usage: java ClassTest {dir} {output}");
return;
}
String dirPath = args[0];
String pathOut = args[1];
}
}
final
关键字意味着该值将 永远不会重新分配 。
static
关键字让变量成为class变量而不是实例变量。
补充说明,一般class常量都是大写加下划线分隔,所以改了名字。
所以如果你想声明它们"globally"最好是使用类似于下面的代码。
public class ClassTest {
public static final String DIR_PATH = "C:/Users/test1/";
public static final String PATH_OUT = "C:/Users/stats.csv";
public static void main(String[] args) {
// Use DIR_PATH or PATH_OUT as needed
}
}
请注意,只有在不同方法中重复使用 DIR_PATH
或 PATH_OUT
变量时,前面的代码才有用。否则,将变量定义为 main 方法的局部变量是正确的,以将可见性限制为仅使用它的代码部分。
试试这个..这是最干净的方法:
public class ClassTest implements StaticPath {
public static void main(String[] args) {
System.out.print(PATH_OUT);
}
}
interface StaticPath {
public final static String PATH = "C:/Users/";
public final static String PATH_OUT = PATH + "stats.csv";
public final static String PATH_IN = PATH + "dyn.csv";
}