好吧,我需要将这些数据用于至少两个类:
String navHome = "home";
String navAbout = "about";
String navUpload = "upload";
String navUploadProc = "uploadProc";
String navStartUpload = "startUpload";
String navContact = "contact";
String navSearch = "search";
String navManual = "manual";
String navBackToGmis = "backToGmis";
我想把它粘贴到其他类中,但它违反了编程原则,因为它重复了同样的事情。文本和XML文件不适合存储数据,继承不是一种选择,因为Java不支持多重继承...任何想法?
是的,我知道你们大多数人都会觉得迟钝,但我需要知道这些。
答案 0 :(得分:3)
如果这些是常量,那么你可以在一个类(或接口)中声明它们并在另一个类中使用它们:
<强> A.java 强>:
public class A {
public static final String navHome = "home";
public static final String navAbout = "about";
public static final String navUpload = "upload";
public static final String navUploadProc = "uploadProc";
public static final String navStartUpload = "startUpload";
public static final String navContact = "contact";
public static final String navSearch = "search";
public static final String navManual = "manual";
public static final String navBackToGmis = "backToGmis";
. . .
}
<强> B.java 强>:
import static A.*; // or list each String in a separate import
public class B {
. . . // code can use nav* as if they were declared in class B
}