国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C# 語言規范--1.7 類

2024-07-21 02:29:59
字體:
來源:轉載
供稿:網友
   類聲明定義新的引用類型。一個類可以從另一個類繼承,并且可以實現多個接口。

    類成員可以包括:常數、字段、方法、屬性、事件、索引器、運算符、實例構造函數、析構函數、靜態構造函數和嵌套類型聲明。每個成員都有關聯的可訪問性,它控制能夠訪問該成員的程序文本區域。有五種可能的可訪問性形式。下表概述了這些形式。

    形式 直觀含義
public
 不限制訪問。
protected
 訪問限于該成員所屬的類或從該類派生來的類型。
internal
 訪問限于此程序。
protected internal
 訪問限于此程序或從該成員所屬的類派生的類型。
private
 訪問限于該成員所屬的類型。

示例

using system;
class myclass
{
   public myclass() {
      console.writeline("instance constructor");
   }
   public myclass(int value) {
      myfield = value;
      console.writeline("instance constructor");
   }
   ~myclass() {
      console.writeline("destructor");
   }
   public const int myconst = 12;
   public int myfield = 34;
   public void mymethod(){
      console.writeline("myclass.mymethod");
   }
   public int myproperty {
      get {
         return myfield;
      }
      set {
         myfield = value;
      }
   }
   public int this[int index] {
      get {
         return 0;
      }
      set {
         console.writeline("this[{0}] = {1}", index, value);
      }
   }
   public event eventhandler myevent;
   public static myclass operator+(myclass a, myclass b) {
      return new myclass(a.myfield + b.myfield);
   }
   internal class mynestedclass
   {}
}
    顯示了一個類,它包含了具有各種可訪問性的成員。示例

class test
{
   static void main() {
      // instance constructor usage
      myclass a = new myclass();
      myclass b = new myclass(123);
      // constant usage
      console.writeline("myconst = {0}", myclass.myconst);
      // field usage
      a.myfield++;
      console.writeline("a.myfield = {0}", a.myfield);
      // method usage
      a.mymethod();
      // property usage
      a.myproperty++;
      console.writeline("a.myproperty = {0}", a.myproperty);
      // indexer usage
      a[3] = a[1] = a[2];
      console.writeline("a[3] = {0}", a[3]);
      // event usage
      a.myevent += new eventhandler(myhandler);
      // overloaded operator usage
      myclass c = a + b;
   }
   static void myhandler(object sender, eventargs e) {
      console.writeline("test.myhandler");
   }
   internal class mynestedclass
   {}
}
    顯示了這些成員的用法。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 车致| 兴和县| 八宿县| 呼图壁县| 保定市| 舟曲县| 凤翔县| 临泉县| 泉州市| 湖北省| 长泰县| 淳安县| 贵港市| 林甸县| 巴东县| 宁海县| 涪陵区| 曲麻莱县| 邵东县| 武汉市| 城市| 康乐县| 尉氏县| 高淳县| 肇东市| 安乡县| 望都县| 金平| 金湖县| 应城市| 淮阳县| 米脂县| 东明县| 五峰| 宁乡县| 塘沽区| 高邑县| 弥渡县| 清流县| 兰溪市| 边坝县|