1. 程式人生 > >17-類

17-類

析構函數 str num app tel 成員 構造函數 ron bsp

1. 構造函數、析構函數

修飾詞的作用域僅一行。

類的默認訪問標識符是 internal,成員的默認訪問標識符是 private

using System;
namespace LineApplication
{
   class Line
   {
      private double length;   // 線條的長度
      public Line()  // 構造函數
      {
         Console.WriteLine("對象已創建");
      }
      ~Line() //析構函數
      {
         Console.WriteLine("對象已刪除");
      }

      public void setLength( double len )
      {
         length = len;
      }
      public double getLength()
      {
         return length;
      }

      static void Main(string[] args)
      {
         Line line = new Line();
         // 設置線條長度
         line.setLength(6.0);
         Console.WriteLine("線條的長度: {0}", line.getLength());           
      }
   }
}

  

2. 靜態成員

public static int num;

  

參考:
http://www.runoob.com/csharp/csharp-class.html

17-類