我正在使用Java对Mysql数据库进行建模。这是我的个人实验。为了让不同的列可以有不同的字符集,我需要字符串来存储表的字符集,并且也需要为每个列存储其字符集。如果列的字符集字段可以指向表的字符集字段,那将非常有帮助。但是我知道Java没有指针。
你有什么想法,如何将一个对象的字段指向另一个对象的字段,以便这两个字段始终匹配?
你有什么想法,如何将一个对象的字段指向另一个对象的字段,以便这两个字段始终匹配?
public class Table {
// name does not store a String, it stores a reference to a String
private String name;
// tableName is not passed in by copy, tableName's reference is passed in.
public Table(String tableName) {
// this is not a copy assignment, but a reference assignment
name = tableName;
}
}
public class Table {
public Column[] columns;
public String name;
public Table() {
name = ...;
columns = ...;
}
}
public class CreateTableDDL {
public String statement(Table table) {
StringBuilder buffer = new StringBuilder();
buffer.append("CREATE TABLE ");
buffer.append(table.name);
buffer.append(" (");
for (int i = 0; i < table.columns.length; i++) {
Column column = table.columns[i];
...
}
...
return buffer.toString();
}
}
columns
公开为类型为 Column
的数组,这本身不是什么坏事,直到我们决定将其作为 List
的一部分以便于在新的漂亮的 TableEditor
中动态添加或删除 Column
。List
接口。实际上,我们需要做更多的工作,因为我们还必须搜索可能直接使用 columns
字段的每个外部库,因为未知于我们的多个 JAR 可能已经使用了这个公共类。columns
相关的工作实际上是 Table
的工作,但位于“helpers”和辅助类中,这些工作应该最好由 Table 本身处理。public class Table {
private Column[] columns;
private String name;
public Table() {
name = ...;
columns = ...;
}
public Column[] getColumns() {
Column[] copy = new Column[columns.length];
for (int i = 0; i < columns.length; i++) {
copy[i] = columns[i].clone();
}
return copy;
}
}
List
,并从该列表构建我们的“向后兼容”列数组。即使我们决定我们之前存在的columns
字段现在需要是一个字符串到数据类型的映射表
,调用代码也不需要进行更改。 public class CreateTableDDL {
public String statement(Table table) {
StringBuilder buffer = new StringBuilder();
buffer.append("CREATE TABLE ");
buffer.append(table.getName());
buffer.append(" (");
for (int i = 0; i < table.getColumns().length; i++) {
Column column = table.getColumn(i);
...
}
...
return buffer.toString();
}
}
a = <valueof 1>; //"1" is now stored in memory, and a is a index to "1" in memory.
b = a; //b now indexes the same memory location as a. They both index the "1" in memory.
a = <value of 2>; //"2" now replaces "1" at the indexed memory location.
output a; //In this case would get the index value the language uses.
output b; //You'd get the same index value as a since they index the same memory location.
output <valueof a>; //Now you get "2", because however it's done in the language you have extracted the data in the memory location indexed by a.
output <valueof b>; //Same output as before as b indexes the same memory location as a.
a = <valueof 1>; //"1" is now stored in memory, and a is a index to "1" in memory.
b = a; //b now indexes a new memory location that now also stores "1".
a = <value of 2>; //"2" now replaces "1" at the indexed memory location.
output a; //You get "2" because a pass by value language will be designed to give you value, not the index.
output b; //You get "1" because when b was assigned to match a, a stored "1". But b is independent of a once assignment is complete.
混淆的原因在于Java使用传递引用机制来实现按值传递设计,某些情况下。对于基本类型,Java采用按值传递方式。对于对象,它采用按引用传递方式。如果将一个基本类型包装在一个对象中(通常建议这样做),它将作为按引用传递方式处理。但是,虽然字符串是对象,但它们也作为按值传递方式处理。但是字符串很奇怪。
Java变量是通过引用传递的。因此,如果您声明了Object obj
,那么obj
将始终是当前作用域中指向同一对象的引用。当您将obj
传递给一个方法时,您并没有传递对象的副本,而是传递了对它的引用。
此外,Java还为实现Iterable
接口的集合提供了迭代器,您可能想要了解这些内容。这些作用类似于指向List
或类似列表中特定位置的指针。
testValue = "test02";
不会改变字符串test01
,而是改变指针testValue
指向字符串"test02"
。正如 Edwin 所说,Java 引用是没有算术运算的指针。 - Mooing Duck