私有类与公共方法?
这是一段代码:
private class myClass
{
public static void Main()
{
}
}
'or'
private class myClass
{
public void method()
{
}
}
我知道,第一个代码行不通。第二个会的。
但是为什么首先不起作用?有什么具体的原因吗?
其实就是从这个角度去寻找解决方案,所以才大胆的。对不起
Here is a piece of code:
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.
But why first is not working? Is there any specific reason for it?
Actually looking for a solution in this perspective, thats why made it bold. Sorry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这种情况下这将是有意义的;您有一个公共类
SomeClass
,您想要在其中封装一些仅与SomeClass
相关的功能。您可以通过在SomeClass
中声明一个私有类(在我的示例中为SomePrivateClass
)来实现此目的,如下所示。无论
SomePrivateClass
是static
还是包含public static
方法,这一点都成立。我将其称为嵌套类,并在另一个StackOverflow 线程。
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 toSomeClass
. You could do this by declaring a private class (SomePrivateClass
in my example) withinSomeClass
, as shown below.This holds true regardless of whether
SomePrivateClass
isstatic
, or containspublic static
methods.I would call this a nested class, and it is explored in another StackOverflow thread.
Richard Ev 给出了一个在嵌套类中进行访问的用例。嵌套类的另一个用例是公共接口的私有实现:
这允许提供公共接口或基类的私有(或受保护或内部)实现。消费者不需要知道也不关心具体的实现。这也可以在没有嵌套类的情况下通过将
MySpecialEnumerator
类设置为内部来完成,因为您不能拥有非嵌套的私有类。BCL 广泛使用非公开实现。例如,LINQ 运算符返回的对象是实现
IEnumerable
的非公共类。Richard Ev gave a use case of access inside a nested classes. Another use case for nested classes is private implementation of a public interface:
This allows one to provide a private (or protected or internal) implementation of a public interface or base class. The consumer need not know nor care about the concrete implementation. This can also be done without nested classes by having the
MySpecialEnumerator
class be internal, as you cannot have non-nested private classes.The BCL uses non-public implementations extensively. For example, objects returned by LINQ operators are non-public classes that implement
IEnumerable<T>
.这段代码在语法上是正确的。但最大的问题是:它有用吗,或者至少在您想要使用它的上下文中可用?可能不是,因为
Main
方法必须位于public
类中。This code is syntactically correct. But the big question is: is it useful, or at least usable in the context where you want to use it? Probably not, since the
Main
method must be in apublic
class.Main()
方法是应用程序执行开始的地方,因此您无法编译第一个类(使用public static void Main()
)的原因是您已经有了应用程序中其他位置的 Main
方法。编译器不知道从哪里开始执行您的应用程序。您的应用程序必须只有一个
Main
方法才能以默认行为进行编译,否则您需要添加 /main 选项。Main()
method is where application execution begin, so the reason you cannot compile your first class (withpublic static void Main()
) is because you already haveMain
method somewhere else in your application. The compiler don't know where to begin execute your application.Your application must have only one
Main
method to compile with default behavior otherwise you need to add /main option when you compile it.