C#.NET中的定义类成员(字段)

/*
 * Created by SharpDevelop.
 * User: noo
 * Date: 2009-8-16
 * Time: 15:28
 * 
 * 定义类成员(字段),所有的成员都有自己的访问级别,public,private(默认),internal(项目内部),protected(类、派生类)
 * 公共关键字static(静态成员)
 
*/

using  System ;
class  fieldA
{
    
public   readonly   int  myInt = 10 ; // 只读字段
     public   static   string  myString = " 静态字段 " ;
//     public static const int a=5; // 常量“fieldA.a”不能标记为 static
     public   const   int  a = 5 ; // const成员本来就就是静态的,所以不能加关键字static
     private   string  str = " 私有字段 " ;
}
class  Test
{
    
static   void  Main()
    {
        fieldA a
= new  fieldA ();
        Console.WriteLine (a.myInt);
        Console.WriteLine (fieldA.a );
// 这个调用方法同静态成员的调用方法
        Console.WriteLine (fieldA.myString );
    }
}

你可能感兴趣的:(.net)