日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

【转】c#中类的默认访问修饰符

發布時間:2025/3/18 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】c#中类的默认访问修饰符 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近去面試,被問到c#中類的默認訪問修飾符,被搞暈了,回來在網上找到一篇好文章,才明白到底是怎么回事。

以下轉自:http://blog.163.com/wangzhenbo85@126/blog/static/10136328220110811312766/

類(class)或結構(struct)如果不是在其它類或結構中的話,它的訪問類型要不就是internal, 要不就是public; 換句話說,如果它在其它類或結構中的話,則可以為private 或protected等。下面我說的類和結構,如無特殊說明,均指非"類中類" 類中所有的成員,默認均為private。?? C#用多種修飾符來表達類的不同性質。根據其保護級C#的類有五種不同的限制修飾符:???? public可以被任意存取;?? protected只可以被本類和其繼承子類存取;?? internal只可以被本組合體(Assembly)內所有的類存取,組合體是C#語言中類被組合后的邏輯單位和物理單位,其編譯后的文件擴展名往往是“.DLL”或“.EXE”。?? protected internal唯一的一種組合限制修飾符,它只可以被本組合體內所有的類和這些類的繼承子類所存取。?? private只可以被本類所存取。?? 如果不是嵌套的類,命名空間或編譯單元內的類只有public和internal兩種修飾。???? new修飾符只能用于嵌套的類,表示對繼承父類同名類型的隱藏。?? override 只能用于嵌套的類,表示對繼承父類同名類型的覆蓋。

abstract用來修飾抽象類,表示該類只能作為父類被用于繼承,而不能進行對象實例化。抽象類可以包含抽象的成員,但這并非必須。abstract不能和new同時用。下面是抽象類用法的偽碼:???? abstract class A? {???? public abstract void F();? }? abstract class B: A? {???? public void G() {}? }? class C: B? {???? public override void F()????? {?? //方法F的實現???? }? }??? 抽象類A內含一個抽象方法F(),它不能被實例化。類B繼承自類A,其內包含了一個實例方法G(),但并沒有實現抽象方法F(),所以仍然必須聲明為抽象類。類C繼承自類B,實現類抽象方法F(),于是可以進行對象實例化。???? sealed用來修飾類為密封類,阻止該類被繼承。同時對一個類作abstract和sealed的修飾是沒有意義的,也是被禁止的。

enum 的默認訪問修飾符:public,且此類型不允許其它訪問修飾符interface的默認訪問修飾符:interal,且此類型不允許其它訪問修飾符;接口的成員默認訪問修飾符是public,也不可能是其他訪問修飾符 委托,默認internal 命名空間上不允許使用訪問修飾符。命名空間沒有訪問限制。(命名空間,枚舉類型成員默認public,也不可能是其他訪問修飾符 //?????? 待驗證)

c# 的訪問修飾符是private 還是 internal? 準確的說,不能一概而論。 [MSDN] Classes and structs that are not nested within other classes or structs can be either public or internal. A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by default unless the keyword public is added to the class definition, as in the previous example. Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself — it always has access to itself and all of its own members.
類(class)或結構(struct)如果不是在其它類或結構中的話,它的訪問類型要不就是internal, 要不就是public; 換句話說,如果它在其它類或結構中的話,則可以為private 或protected等。下面我說的類和結構,如無特殊說明,均指非"類中類" 類或結構的默認訪問類型是internal. 類中所有的成員,默認均為private。

[MSDN] Interfaces, like classes, can be declared as public or internal types. Unlike classes, interfaces default to internal access. Interface members are always public, and no access modifiers can be applied. Namespaces and enumeration members are always public, and no access modifiers can be applied. Delegates have internal access by default. Any types declared within a namespace or at the top level of a compilation unit (for example, not within a namespace, class, or struct) are internal by default, but can be made public.?

接口默認訪問符是internal 接口的成員默認訪問修飾符是public,也不可能是其他訪問修飾符 命名空間,枚舉類型成員默認public,也不可能是其他訪問修飾符 委托,默認internal 在命名空間內部或編譯單元頂部的所有類型,默認是internal,可以人為改為public。

總結

以上是生活随笔為你收集整理的【转】c#中类的默认访问修饰符的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。