私有类公共方法? [英] Private class with Public method?
问题描述
下面是一段代码:
private class myClass
{
public static void Main()
{
}
}
'or'
private class myClass
{
public void method()
{
}
}
我知道,第一个将无法正常工作。而第二个会。
I know, first one will not work. And second one will.
但是为什么第一次不工作?是否有任何具体的原因是什么?
其实寻找这个角度来看的解决方案,这就是为什么使它大胆。对不起
Actually looking for a solution in this perspective, thats why made it bold. Sorry
推荐答案
这将是在这种情况下有意义的;你有一个公共类 SomeClass的
,里面要封装了一些功能,只对 SomeClass的
相关。您可以通过在 SomeClass的
声明(在我的例子 SomePrivateClass
)的私有类,如下图所示做到这一点。
It would be meaningful in this scenario; you have a public class SomeClass
, inside which you want to encapsulate some functionality that is only relevant to SomeClass
. You could do this by declaring a private class (SomePrivateClass
in my example) within SomeClass
, as shown below.
public class SomeClass
{
private class SomePrivateClass
{
public void DoSomething()
{
}
}
// Only SomeClass has access to SomePrivateClass,
// and can access its public methods, properties etc
}
这无论 SomePrivateClass <中也是如此/ code>是
静态
,或者包含公共静态
方法。
This holds true regardless of whether SomePrivateClass
is static
, or contains public static
methods.
我称之为的嵌套类的,它是探索在其他的计算器线程。
I would call this a nested class, and it is explored in another StackOverflow thread.
这篇关于私有类公共方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!