簡體   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