【发布时间】:2011-11-20 11:37:55
【问题描述】:
即使我公开了类成员函数并且它被 其他客户端应用程序的实现细节 功能永远不会暴露给客户。为什么要使成员函数受保护或私有?
例如,如果我的课程是数学,使用公共函数 sum(int, int b),那么只有接口/声明部分会暴露给客户端,而不是实现。
public class Math
{
public int sum(int, int b)
{
//Implementation
}
}
public class Client
{
Math objMath = new Math();
objMath.Sum(4,10);//It will not display implementation inside sum than why to avoid
}
【问题讨论】:
-
不好的例子。
Math可以/应该是static -
除了这里的优秀答案,在维基百科和其他地方查找松散耦合。这是 OO 的主要好处之一。
-
@Henk 那么我该如何扩展它以提供更快的添加
4和10的实现? :)