欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#類(lèi)中的屬性使用總結(jié)(詳解類(lèi)的屬性)

 更新時(shí)間:2014年03月21日 14:16:14   作者:  
屬性是一種類(lèi)的成員,它的實(shí)現(xiàn)類(lèi)似函數(shù),訪(fǎng)問(wèn)類(lèi)似字段。它的作用是提供一種靈活和安全的機(jī)制來(lái)訪(fǎng)問(wèn),修改私有字段。所以屬性必須依賴(lài)于字段

復(fù)制代碼 代碼如下:

private int dd; 
public int dd 

    get{ return xx*3;} 
    set{ xx = value/3;} 

沒(méi)有set的屬性是一種只讀屬性,沒(méi)有g(shù)et的訪(fǎng)問(wèn)器是一種只寫(xiě)屬性。
(1) get訪(fǎng)問(wèn)器用來(lái)返回字段或者計(jì)算 并返回字段,它必須以return或者throw終結(jié)。
復(fù)制代碼 代碼如下:

private string name; 
public string Name 

    get 
    { 
        return name != null ? name : "NA"; 
    } 

(2) set訪(fǎng)問(wèn)器類(lèi)似返回類(lèi)型為void的函數(shù),使用value的隱式參數(shù)
復(fù)制代碼 代碼如下:

private string name;  // the name field 
    public string Name    // the Name property 
    { 
        get 
        { 
            return name; 
        } 
        set 
        { 
            name = value; 
        } 
    }

(3) 訪(fǎng)問(wèn)器的限制
屬性訪(fǎng)問(wèn)標(biāo)記可以為public,private,protected,internal,protected internal,因?yàn)樵L(fǎng)問(wèn)器的訪(fǎng)問(wèn)限制必須比屬性的訪(fǎng)問(wèn)限制更加嚴(yán)格,所以
復(fù)制代碼 代碼如下:

private int xx; 
      public int sxx 
      { 
          public get { return xx; }//error 
          set { xx = value; } 
      } 

不能對(duì)接口或者顯式的接口使用訪(fǎng)問(wèn)修飾符,因?yàn)榻涌诶锢锩嫠械哪J(rèn)是public的;
同時(shí)具有g(shù)et,set訪(fǎng)問(wèn)器時(shí),才允許使用訪(fǎng)問(wèn)修飾符,并且只能有一個(gè)使用;
如果屬性有override修飾的時(shí)候,訪(fǎng)問(wèn)器修飾符必須與被重寫(xiě)的匹配。
訪(fǎng)問(wèn)器的可訪(fǎng)問(wèn)級(jí)別必須比屬性的可訪(fǎng)問(wèn)級(jí)別更加嚴(yán)格

理解:
首先第四條最容易想到,也是很合理的,畢竟是外圍的決定內(nèi)部的。
其次,既然第四條可以理解,那么如果只有一個(gè)訪(fǎng)問(wèn)器的時(shí)候,訪(fǎng)問(wèn)器訪(fǎng)問(wèn)級(jí)別等同屬性,如果這個(gè)時(shí)候再去指  定更加嚴(yán)格的訪(fǎng)問(wèn)級(jí)別,那么為何不當(dāng)初在屬性上指定呢?
      這條理解了,那么為什么必須同時(shí)具有g(shù)et,set才能添加訪(fǎng)問(wèn)修飾符就更加明確了。

推理:
接口中屬性是public的,那么如果接口中只有一個(gè)get或者set的時(shí)候,我們可以在繼承中指明另一個(gè)訪(fǎng)問(wèn)器的屬   性。但是如果接口中同時(shí)具有g(shù)et,set,那么按照派生和繼承的匹配性,這時(shí)候就不能這樣再指定訪(fǎng)問(wèn)器的訪(fǎng)問(wèn)限制了。

復(fù)制代碼 代碼如下:

public interface ISomeInterface 

    int TestProperty 
    { 
        // No access modifier allowed here 
        // because this is an interface. 
        get; 
    } 


 
public class TestClass : ISomeInterface 

    public int TestProperty 
    { 
        // Cannot use access modifier here because 
        // this is an interface implementation. 
        get { return 10; } 

 
        // Interface property does not have set accessor, 
        // so access modifier is allowed. 
        protected set { } 
    } 

(4)可以用static 修飾屬性,以便隨時(shí)訪(fǎng)問(wèn)

復(fù)制代碼 代碼如下:

private static int counter; 
public static int Counter 
    { 
        get { return counter; } 
    } 

(5)屬性隱藏
復(fù)制代碼 代碼如下:

public class Employee 

    private string name; 
    public string Name 
    { 
        get { return name; } 
        set { name = value; } 
    } 


 
public class Manager : Employee 

    private string name; 

 
    // Notice the use of the new modifier: 
    public new string Name // use new to hide property in base class 
    { 
        get { return name; } 
        set { name = value + ", Manager"; } 
    } 

(6)virtual來(lái)修飾屬性,派生類(lèi)使用override來(lái)重寫(xiě)屬性

復(fù)制代碼 代碼如下:

public class Parent 

    public virtual int TestProperty 
    { 

        protected set { } 
        get { return 0; } 
    } 

public class Kid : Parent 

    public override int TestProperty 
    { 
        protected set { } 
        get { return 0; } 
    } 

(7) abstract 修飾屬性,派生類(lèi)來(lái)實(shí)現(xiàn)屬性

復(fù)制代碼 代碼如下:

abstract class Shape 

    public abstract double Area 
    { 
        get; 
        set; 
    } 


 
class Square : Shape 

    public double side; 
    public override double Area 
    { 
        get 
        { 
            return side * side; 
        } 
        set 
        { 
            side = System.Math.Sqrt(value); 
        } 
    } 

(8)sealed 修飾屬性,派生類(lèi)不能修改屬性

(9)接口屬性
接口屬性不具有函數(shù)體

復(fù)制代碼 代碼如下:

public interface Inters 

    string Name 
    {    
        get; 
        set; 
    } 

(10) 自動(dòng)屬性
當(dāng)屬性訪(fǎng)問(wèn)器中不需要其他訪(fǎng)問(wèn)邏輯時(shí)候,就可以使用自動(dòng)屬性,使代碼更加簡(jiǎn)潔

復(fù)制代碼 代碼如下:

public double TotalPurchases { get; set; } 
public string Name { get; set; } 
public int CustomerID { get; set; }

相關(guān)文章

最新評(píng)論