-2

好吧,我需要这些数据至少在两个类中使用:

   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 不支持多重继承......有什么想法吗?

是的,我知道这对你们大多数人来说听起来很迟钝,但我需要这方面的提示。

4

1 回答 1

3

如果这些是常量,那么您可以在一个类(或接口)中声明它们并在另一个类(或接口)中使用它们:

一个.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
}
于 2013-04-23T00:43:45.360 回答