While using C#, I recently realised that I can call a Foo
object's private functions from Foo
's static functions, and even from other Foo
objects. After everything I have learned about access modifiers, this sounds very strange to me.
As far as I know, you make a function private when it does something that's part of some kind of internal process. Only the object itself knows when to use those functions, because other objects shouldn't/can't control the object's flow. Is there any reason why other objects of the same class should be excepted from this pretty straightforward rule?
As per request, an example:
public class AClass {
private void doSomething() { /* Do something here */ }
public void aFunction() {
AClass f = new AClass();
f.doSomething(); // I would have expected this line to cause an access error.
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…