私有类与公共方法?

发布于 2024-12-09 17:52:33 字数 295 浏览 0 评论 0原文

这是一段代码:

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有心的人 2024-12-16 17:52:33

在这种情况下这将是有意义的;您有一个公共类 SomeClass,您想要在其中封装一些仅与 SomeClass 相关的功能。您可以通过在 SomeClass 中声明一个私有类(在我的示例中为 SomePrivateClass)来实现此目的,如下所示。

public class SomeClass
{
    private class SomePrivateClass
    {
        public void DoSomething()
        {

        }
    }

    // Only SomeClass has access to SomePrivateClass,
    // and can access its public methods, properties etc
}

无论 SomePrivateClassstatic 还是包含 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 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
}

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.

情域 2024-12-16 17:52:33

Richard Ev 给出了一个在嵌套类中进行访问的用例。嵌套类的另一个用例是公共接口的私有实现:

public class MySpecialCollection<T> : IEnumerable<T>
{ 
    public IEnumerator<T> GetEnumerator()
    {
        return new MySpecialEnumerator(...);
    }

    private class MySpecialEnumerator : IEnumerator<T>
    {
        public bool MoveNext() { ... }
        public T Current
        { 
            get { return ...; }
        }
        // etc...
    } 
}

这允许提供公共接口或基类的私有(或受保护或内部)实现。消费者不需要知道也不关心具体的实现。这也可以在没有嵌套类的情况下通过将 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:

public class MySpecialCollection<T> : IEnumerable<T>
{ 
    public IEnumerator<T> GetEnumerator()
    {
        return new MySpecialEnumerator(...);
    }

    private class MySpecialEnumerator : IEnumerator<T>
    {
        public bool MoveNext() { ... }
        public T Current
        { 
            get { return ...; }
        }
        // etc...
    } 
}

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>.

我只土不豪 2024-12-16 17:52:33

这段代码在语法上是正确的。但最大的问题是:它有用吗,或者至少在您想要使用它的上下文中可用?可能不是,因为 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 a public class.

一袭白衣梦中忆 2024-12-16 17:52:33

Main() 方法是应用程序执行开始的地方,因此您无法编译第一个类(使用 public static void Main())的原因是您已经有了 应用程序中其他位置的 Main 方法。编译器不知道从哪里开始执行您的应用程序。

您的应用程序必须只有一个 Main 方法才能以默认行为进行编译,否则您需要添加 /main 选项。

Main() method is where application execution begin, so the reason you cannot compile your first class (with public static void Main()) is because you already have Main 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文