17-类

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

猜你喜欢

转载自www.cnblogs.com/alexYuin/p/9068558.html
今日推荐