我正在重新设计一个我一直在编写的应用程序,为此,我目前将表定义分为不同的接口(interface),每个表一个,然后是整个数据库的整体接口(interface)。
这样做时,我在整个界面中使用了一些在表界面中使用的通用常量。总体而言,接口(interface)还利用表接口(interface)中的常量。
我已经使用它编译并成功运行了一个应用程序(基本上是无处不在的 Hello World 应用程序,只需额外两行代码),一切似乎都很好。我实际上对它的编译感到非常惊讶。
基本上,我是否走上了一条注定会引发问题的道路。
这是一些示例代码:-
接口(interface)DBAislesTableConstants 注意!减少到只有 1 行
import static mjt.shopwise.DBConstants.DEFAULTORDER;
import static mjt.shopwise.DBConstants.IDTYPE;
import static mjt.shopwise.DBConstants.INT;
import static mjt.shopwise.DBConstants.PERIOD;
import static mjt.shopwise.DBConstants.STD_ID;
import static mjt.shopwise.DBConstants.TXT;
public interface DBAislesTableConstants {
String AISLES_ID_COL = STD_ID;
String AISLES_ID_COL_FULL = AISLES_TABLE +
PERIOD +
AISLES_ID_COL;
String AISLES_ALTID_COL = AISLES_TABLE + STD_ID;
String AISLES_ALTID_COL_FULL = AISLES_TABLE +
PERIOD +
AISLES_ALTID_COL;
String AISLES_ID_TYPE = IDTYPE;
Boolean AISLES_ID_PRIMARY_INDEX = true;
DBColumn AISLESIDCOL = new DBColumn(AISLES_ID_COL,
AISLES_ID_TYPE,
AISLES_ID_PRIMARY_INDEX,
""
);
ArrayList<DBColumn> AISLESCOLS = new ArrayList<>(Arrays.asList(AISLESIDCOL));
DBTable AISLESTABLE = new DBTable(AISLES_TABLE,AISLESCOLS);
}
DBColum 和 DBTable 是已定义的类,其方法用于构建我可以用来创建和更新实际数据库的内部架构。
接口(interface)DBConstants是:-
import java.util.ArrayList;
import java.util.Arrays;
import static mjt.shopwise.DBAislesTableConstants.AISLESTABLE;
import static mjt.shopwise.DBProductsTableConstants.PRODUCTSTABLE;
import static mjt.shopwise.DBShopsTableConstants.SHOPSTABLE;
interface DBConstants {
String DATABASE_NAME = "ShopWise";
String STD_ID = "_id";
String PERIOD = ".";
String INT = "INTEGER";
String TXT = "TEXT";
String IDTYPE = INT;
String DEFAULTORDER = "1000";
ArrayList<DBTable> SHOPWISETABLES = new ArrayList<>(Arrays.asList(
AISLESTABLE));
DBDatabase SHOPWISE = new DBDatabase(DATABASE_NAME,SHOPWISETABLES);
}
DBDatabase 是整个数据库的定义类,例如下面我使用generateExportSchemaSQL方法。
为了测试这一点,我使用了以下命令,它创建了 SQL 来创建所有表(运行该命令并将 SQL 放入 SQLite Manager 中,然后它创建了表)。 stophere 只是用作断点,以便我可以检查和复制 SQL。
String sql = DBConstants.SHOPWISE.generateExportSchemaSQL();
int stophere = 0;
最佳答案
从语法上讲,交叉引用类(或接口(interface),就此而言)不是问题。
我做了一个测试程序。所有类都在同一个包中。使用IntelliJ IDEA 2016.2.5和Oracle JDK 1.8.0_112,编译无警告,运行无异常:
class ClassOne {
static final String CONSTANT_ONE = ClassTwo.CONSTANT_TWO;
}
class ClassTwo {
static final String CONSTANT_TWO = ClassOne.CONSTANT_ONE;
}
interface InterfaceOne {
String VALUE_ONE = InterfaceTwo.VALUE_TWO;
}
interface InterfaceTwo {
String VALUE_TWO = InterfaceOne.VALUE_ONE;
}
public class Main {
public static void main(String[] args) {
System.out.println(InterfaceOne.VALUE_ONE == null);
System.out.println(InterfaceTwo.VALUE_TWO == null);
System.out.println(ClassOne.CONSTANT_ONE == null);
System.out.println(ClassTwo.CONSTANT_TWO == null);
}
}
输出为:
true
true
true
true
从而导致语义问题。虽然 Java 允许循环引用类甚至其字段,但在运行时它们将为 null
,这很可能不是开发人员想要分配给字段的预期值。
总之,您应该注意字段中的循环引用。
顺便说一句,您应该在类而不是接口(interface)中定义常量。看这个问题:What is the use of interface constants?
关于java - 交叉引用接口(interface)是否会出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624970/