我想知道关于类初始化的最佳实践是什么,
我的意思是我应该Customer c = new Customer();
在顶层初始化一个类,然后在类的任何地方使用它:
Tools tools = new Tools();
public boolean doCIdCheck(int cId) {
final Iterator<Customer> cursor = tools.Customers.iterator();
while (cursor.hasNext()) {
if (cursor.next().getCIdCheck(cId)) {
return true;
}
}
return false;
}
或者我应该new Customer().checkCId();
在需要的地方使用它:
public boolean doCIdCheck(int cId) {
final Iterator<Customer> cursor = new Tools().Customers.iterator();
while (cursor.hasNext()) {
if (cursor.next().getCIdCheck(cId)) {
return true;
}
}
return false;
}
或者最好让每个函数/方法都有自己的类实例:
public boolean doCIdCheck(int cId) {
Tools tools = new Tools();
final Iterator<Customer> cursor = tools.Customers.iterator();
while (cursor.hasNext()) {
if (cursor.next().getCIdCheck(cId)) {
return true;
}
}
return false;
}