简体   繁体   English

public static class vs static class

[英]public static class vs static class

Let say I have this class and all method are properly implemented (in this case I think the implementation is irrevelant to the question). 假设我有这个类,并且所有方法都已正确实现(在这种情况下,我认为实现对该问题是无关紧要的)。

static class ZedGraphHelper
{
    public static ZedGraph.ZedGraphControl GetZedGraph(Guid config, Guid equip)
    { throw new NotImplementedException; }

    //This method here is the faulty one
    public static void AdjustGraphParam(ZedGraph.ZedGraphControl zGraph, RP.mgrRPconfigGraph mgr)
    { throw new NotImplementedException; }

    public static void FillGraph(ZedGraph.ZedGraphControl zGraph, Guid config, Guid equip, Guid form)
    { throw new NotImplementedException; }

    public static void FillGraph(ZedGraph.ZedGraphControl zGraph,  Shadow.dsEssais.FMdocDataTable dtDoc, Shadow.dsEssais.FMchampFormDataTable dtChamp)
    { throw new NotImplementedException; }

    public static void LoadDoc(Shadow.dsEssais.FMdocDataTable dtDoc, Guid equip, Guid form)
    { throw new NotImplementedException; }

    public static double LoadDonnee(Guid champ, Guid doc)
    { throw new NotImplementedException; }

    public static SqlDataReader ReadDonnee(Guid champ, Guid doc)
    { throw new NotImplementedException; }
}

this code compile fine and set no error. 这段代码编译好并且没有设置错误。 How ever if I change the class declaration from 如果我改变类声明怎么样?

static class ZedGraphHelper

to

public static class ZedGraphHelper

I got the folowing error message : Inconsistent accessibility: parameter type 'RP.mgrRPconfigGraph' is less accessible than method 'Shadow.ZedGraphHelper.AdjustGraphParam(ZedGraph.ZedGraphControl, RP.mgrRPconfigGraph)' this method is present in the class declaration I have included just here. 我得到了以下错误消息: Inconsistent accessibility: parameter type 'RP.mgrRPconfigGraph' is less accessible than method 'Shadow.ZedGraphHelper.AdjustGraphParam(ZedGraph.ZedGraphControl, RP.mgrRPconfigGraph)'此方法存在于我已包含的类声明中这里。 The method is public static void . 该方法是public static void

Why am I getting this error? 为什么我收到此错误? And does the public change anything in the code behaviour? 公众会改变代码行为中的任何内容吗?

Yes RP.mgrRPconfigGraph is an internal type(or less accessible than that). RP.mgrRPconfigGraph是一种内部类型(或者不太容易访问)。 So when you change ZedGraphHelper to public it exposes its methods as public which are all marked as public . 因此,当您将ZedGraphHelper更改为public它会将其方法公开为public,这些方法都标记为public which you can't do for AdjustGraphParam method since parameter is internal type 由于参数是internal type ,因此您无法对AdjustGraphParam方法执行此操作

Either make the method internal 要么将方法设为内部

internal static void AdjustGraphParam(ZedGraph.ZedGraphControl zGraph, RP.mgrRPconfigGraph mgr)
{ throw new NotImplementedException; }

Or mark the RP.mgrRPconfigGraph type as public 或者将RP.mgrRPconfigGraph类型标记为public

The default access modifier for a class is internal . 类的默认访问修饰符是internal this means that, if you omit the access modifier, the class will be internal. 这意味着,如果省略访问修饰符,则该类将是内部的。

If you change the class to be public, you get this error because one of the parameters of the methods that exists in your class is an internal type. 如果将类更改为公共类,则会出现此错误,因为类中存在的方法之一是内部类型。

This means that your class cannot be public, since it depends on an internal type, which is less accessible than your class. 这意味着您的班级不能公开,因为它取决于内部类型,这种类型比您的类更难访问。 (The internal type can only be used in the assembly in which it has been declared while the public class can be used everywhere). (内部类型只能在声明它的程序集中使用,而公共类可以在任何地方使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM