繁体   English   中英

public static class vs static class

[英]public static class vs static class

假设我有这个类,并且所有方法都已正确实现(在这种情况下,我认为实现对该问题是无关紧要的)。

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; }
}

这段代码编译好并且没有设置错误。 如果我改变类声明怎么样?

static class ZedGraphHelper

public static class ZedGraphHelper

我得到了以下错误消息: Inconsistent accessibility: parameter type 'RP.mgrRPconfigGraph' is less accessible than method 'Shadow.ZedGraphHelper.AdjustGraphParam(ZedGraph.ZedGraphControl, RP.mgrRPconfigGraph)'此方法存在于我已包含的类声明中这里。 该方法是public static void

为什么我收到此错误? 公众会改变代码行为中的任何内容吗?

RP.mgrRPconfigGraph是一种内部类型(或者不太容易访问)。 因此,当您将ZedGraphHelper更改为public它会将其方法公开为public,这些方法都标记为public 由于参数是internal type ,因此您无法对AdjustGraphParam方法执行此操作

要么将方法设为内部

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

或者将RP.mgrRPconfigGraph类型标记为public

类的默认访问修饰符是internal 这意味着,如果省略访问修饰符,则该类将是内部的。

如果将类更改为公共类,则会出现此错误,因为类中存在的方法之一是内部类型。

这意味着您的班级不能公开,因为它取决于内部类型,这种类型比您的类更难访问。 (内部类型只能在声明它的程序集中使用,而公共类可以在任何地方使用)。

暂无
暂无

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

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