如果没有这些 类 在同一个文件中,下面的情况会是什么样子?
How would the following situation look like, without having these classes in the same file?
大家好,
正如标题所说;我想知道如果没有这些 classes 在同一个文件中,下面的情况会是什么样子。
这个例子来自于:'using a table in a different class' specific thread, 第 4 个答案给出。
class A {
private JTable myJTable;
public JTable getMyJTable() {
return myJTable;
}
public void setMyJTableValue(Object value) {
// set the value accordingly
}
}
class B {
private A a;
public void methodWithAccessToA() {
// business logic ...
a.setMyJTableValue(myBusinessValue);
// ...
a.getMyJTable().setValue(myBusinessValue);
}
}
如果这是两个不同文件中的两个 class,这意味着 class B 需要先创建 A 的新 object-instance(a = 新 A),这将让它失去更新的可能性 table 需要更新的内容。
喜欢这个场景:
separated files
那么处理这种情况的最佳方法是什么?我所能想到的就是创建一个 Main class,其中包含并传递来自 class A 和 B 的 created objects,但我认为我可能站在错误的一边。
要将它们放在不同的文件中,您必须声明 classes public。那么每个 class 都必须在 name-of-the-class.java
中
我们一直在通过对我之前回答的评论进行交流,我意识到这不是 pseudo-code。我会评论你的代码。
/** This is a new class definition, by declaring it public,
it has to be in it's own file called A.java */
public class A {
// create an instantiation of JTable, call it myTable
private JTable myJTable = new JTable();
public JTable getMyJTable() {
return myJTable;
}
// is someone calls this method, they override my myTable value.
public void setMyJTableValue(JTable value) {
// Someone is telling me what to set myTable to.
this.myTable = value;
}
}
/** This is a new class definition, by declaring it public,
it has to be in it's own file called B.java*/
public class B {
// This creates a new class A in class B, you are calling it "a"
private A a = new A();
public void methodWithAccessToA() {
// business logic ...
// Create class B's version of a JTable, tell class A to use it
JTable myBusinessValue = new JTable();
a.setMyJTableValue(myBusinessValue);
// ... This gets a JTable from a, then calls the setValue() on the JTable
a.getMyJTable().setValue(whatever JTable is expecting);
}
}
大家好,
正如标题所说;我想知道如果没有这些 classes 在同一个文件中,下面的情况会是什么样子。
这个例子来自于:'using a table in a different class' specific thread, 第 4 个答案给出。
class A {
private JTable myJTable;
public JTable getMyJTable() {
return myJTable;
}
public void setMyJTableValue(Object value) {
// set the value accordingly
}
}
class B {
private A a;
public void methodWithAccessToA() {
// business logic ...
a.setMyJTableValue(myBusinessValue);
// ...
a.getMyJTable().setValue(myBusinessValue);
}
}
如果这是两个不同文件中的两个 class,这意味着 class B 需要先创建 A 的新 object-instance(a = 新 A),这将让它失去更新的可能性 table 需要更新的内容。
喜欢这个场景: separated files
那么处理这种情况的最佳方法是什么?我所能想到的就是创建一个 Main class,其中包含并传递来自 class A 和 B 的 created objects,但我认为我可能站在错误的一边。
要将它们放在不同的文件中,您必须声明 classes public。那么每个 class 都必须在 name-of-the-class.java
中我们一直在通过对我之前回答的评论进行交流,我意识到这不是 pseudo-code。我会评论你的代码。
/** This is a new class definition, by declaring it public,
it has to be in it's own file called A.java */
public class A {
// create an instantiation of JTable, call it myTable
private JTable myJTable = new JTable();
public JTable getMyJTable() {
return myJTable;
}
// is someone calls this method, they override my myTable value.
public void setMyJTableValue(JTable value) {
// Someone is telling me what to set myTable to.
this.myTable = value;
}
}
/** This is a new class definition, by declaring it public,
it has to be in it's own file called B.java*/
public class B {
// This creates a new class A in class B, you are calling it "a"
private A a = new A();
public void methodWithAccessToA() {
// business logic ...
// Create class B's version of a JTable, tell class A to use it
JTable myBusinessValue = new JTable();
a.setMyJTableValue(myBusinessValue);
// ... This gets a JTable from a, then calls the setValue() on the JTable
a.getMyJTable().setValue(whatever JTable is expecting);
}
}