我有方法設置爲public,因爲它們必須由外部類調用,但我只想讓它們通過一個或兩個方法調用。被其他方法調用可能會在我的程序中產生錯誤。因此,爲了阻止我在我自己的方法避免發生意外編程,我一直在做這樣的東西,而我想限制呼叫者的方法中:Java - 通過什麼方法可以被稱爲
if(trace.length<2){
throw new Exception("Class should not call its own function.");
}else if(trace[1].getClassName()!=desiredClassName || trace[1].getMethodName()!=desiredMethodName){
throw new Exception(trace[1].getClassName()+"\" is invalid function caller. Should only be called by "+desiredClassName+"->"+desiredMethodName+".");
}
有別的我應該做的,或者我應該忘記我的程序是如何工作的嗎?
寫javadocs? – BalusC 2011-01-22 05:59:18
聽起來好像我應該開始這樣做,但是我仍然在想,如果有人像我描述的那樣編程來明確地阻止不正確的呼叫,或者如果有人發現這些散佈在我的代碼中,我會只是笑了起來。 – schnozzinkobenstein 2011-01-22 06:08:10