電子書: C#教程學習筆記

1.靜態和非靜態的方法
        靜態成員是屬於類所有的,而非靜態成員則屬於類的實例(對象)所有.
        非靜態方法可以訪問類中的任何成員,而靜態方法只能訪問類中的靜態成員.
  看一個例子:

calss A
{
    int    x;

    static  int    y;

    static int F()
    {
             x = 1;  //錯誤,這個靜態方法F() 不能訪問一個非靜態成員
             y = 2;  //正確,  靜態方法只能訪問類中的靜態成員
    }
}

原因:
        因爲 x 是非靜態成員,在類的每個實例中都佔有一個存儲,  而 靜態方法是類所共享的,它無法

判斷出當前的 x 是屬於哪個類的實例,所以不知道應該到哪個地址去讀取當前X的值.    而y 是非靜態成

員,所有的類實例都公用一個副本,故可以訪問.


        但是又並是不說靜態方法就無法識別非靜態成員了,在C#中,我們可以靈活的採用 傳遞參數 的辦

法.

下面看一個例子:

  public string m_cation;

  public static string GetWindowCaption(Window w)
{
 return w.m_caption;    //這裏的m_caption是一個非靜態成員"窗口的標題"
}
這裏通過參數 w 將對象傳遞給方法執行,這樣它就可以通過具體的類的實例指明調用的對象,這時它可以

訪問具體實例中的成員,無論是靜態還是非靜態的.


對於只讀字段,我們只能在字段的定義中和這強制性屬類的構造函數中進行修改,在其他情況下,字段是"只

讀"的.

public static readonly int x=1;
public const int x=1;
這兩個是等效的.

 

實例我用的是控制檯程序
Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using testStudent;
using System.Net;//getIP類中要用到的命名空間;

namespace testStudent
{
    
class Program
    
{
        
...
        }


        
...

        
class getIp
        
{
            IPAddress[] m_arrayIPs;
            
public void Resolve(string s_host)
            
{
                IPHostEntry ip 
= Dns.GetHostByName(s_host);
                m_arrayIPs 
= ip.AddressList;
            }


            
public IPAddress this[int nIndex]
            
{
                
get 
                    
return m_arrayIPs[nIndex];
                }

            }


            
public int IPLength
            
{
                
get {
                    
return m_arrayIPs.Length;
                }

            }

        }


        
static void Main(string[] args)
        
{
            student s1 
= new student("jiao",21,48);
            student s2 
= new student("wgf",23,65);
            Console.Write(
"年齡最大的是:"+s1.max(s1.s_age,s2.s_age));
            Console.Write(
"體重最大的是:"+s1.max(s1.s_weight,s2.s_weight));

            Console.WriteLine(
" 這裏是一個玩家遊戲:");
            Player man 
= new Player();
            man.Show();
            man
++;
            Console.WriteLine(
"玩家升級後的屬性顯示:");
            man.Show();

            Console.WriteLine(
"下面這個是一個二元操作符重載的例子:");
            DKR d1 
= new DKR(1,2,3);
            DKR d2 
= new DKR(1,2,3);
            DKR d3 
= d1 + d2;
            Console.WriteLine(
"d3的值:d1.x+d2.x={0},d1.y+d2.y={1},d1.z+d2.z={2}",d3.x,d3.y,d3.z);
            Console.WriteLine(
"DKR中的常量xxx={0}",DKR.xxx);
            Console.WriteLine();

            Console.WriteLine(
"下面這裏將通過域名獲取IP:");
            getIp getIp1 
= new getIp();
            getIp1.Resolve(
"www.sohu.com");
            
int n = getIp1.IPLength;
            Console.WriteLine(
"現在將獲取主機的IP:");
            Console.WriteLine();
            
for (int i = 0; i < n;i++ )
            
{
                Console.WriteLine(getIp1[i]);
            }

            Console.ReadKey();
            
        }

      
    }

}



student.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace testStudent
{
    
class student
    
{
        
public string s_name;
        
public int s_age;
        
public float s_weight;

        
public student(string n, int age, float weight)
        
{
            s_name 
= n;
            s_age 
= age;
            s_weight 
= weight;
        }


        
public int max(int x, int y)
        
{
            
if (x > y)
            
{
                
return x;
            }

            
else
            
{
                
return y;
            }

        }

        
public float max(float x, float y)
        
{
            
if (x > y)
            
{
                
return x;
            }

            
else
            
{
                
return y;
            }

        }

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章