假设我们有一个只有一个函数的类,例如:计算DFS。以下哪种方法是首选?为什么?
(暂无翻译)
(暂无翻译)
Approach 1:
public class DFS {
public DFS(Graph g) {
dfs(g); // <--- computation invoked from constructor.
}
private void DFS(Graph g) {
// do dfs traversal
}
}
Client:
DFS dfs = new DFS(graph);
Approach 2:
public class DFS {
Graph g;
public DFS(Graph g) {
this.g = g
}
private void doDFS() {
// do dfs traversal
}
}
Client:
DFS dfs = new DFS(graph);
dfs.doDFS();