假設我有一個要實例化的類。我在類中有幾個私有的“幫助器”方法,它們不需要訪問任何類成員,而僅對它們的參數進行操作,并返回結果。public class Example { private Something member; public double compute() { double total = 0; total += computeOne(member); total += computeMore(member); return total; } private double computeOne(Something arg) { ... } private double computeMore(Something arg) {... } } 有沒有指定任何特別的原因computeOne,并computeMore為靜態方法-或任何特別的理由不?將它們設置為非靜態無疑是最容易的,即使它們可以肯定是靜態的而不會引起任何問題。
3 回答
紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
如果member是您要處理的對象專用的實例變量,那么為什么要將其作為參數傳遞呢?
例如:
public class Example {
private Something member;
public double compute() {
double total = 0;
total += computeOne();
total += computeMore();
return total;
}
private double computeOne() { /* Process member here */ }
private double computeMore() { /* Process member here */ }
}
添加回答
舉報
0/150
提交
取消
