c#-關於自動屬性的思考,

來源:互聯網
上載者:User

c#-關於自動屬性的思考,
自動屬性是什麼

自動屬性就是寫的時候唯寫一個屬性,並不寫欄位,一般情況下也不實現。

為什麼要用自動屬性

下面的程式一測試了一下在一般的類中使用自動屬性的情況;發現編譯器會在產生解決方案的時候會自動產生一個私人的欄位,這個可以在一些反編譯軟體裡都可以看到。在類中使用自動屬性的目的就在於可以不用寫欄位了,但是問題又出現了,既然你自動屬性裡面沒有什麼實現,為什麼不直接用一個public的欄位呢,連set,get和一對花括弧兩個分號都省了呢?想了想大概是編程習慣的原因,人們習慣調用大寫的屬性,而不是小寫欄位,用屬性來保護欄位,那麼為什麼不把欄位大寫呢,同樣也是編程習慣問題,推薦欄位寫成小寫,前面還有個底線,編程習慣統一起來容易理解項目。

如果把自動欄位實現一下會怎麼樣

用問題來驅動我們學習比較好,實現一下怎麼樣,會不會爆炸?我試了一下把程式一的自動屬性Age實現了一下,發現必須同時實現get方法和set方法,如果只實現其中一個就會編譯不通過,我猜實現set和get方法之後這個屬性就不是一個屬性了,只能是一個程式碼片段,程式碼片段裡面有兩個方法,因為實現之後反編譯就沒有自動產生欄位。如果只實現一個另一個沒有實現就會提示類不是抽象類別什麼的,不是抽象類別就不能不實現方法,而又沒有產生欄位。說明這時候編譯器認為這隻是一個代碼塊了。

那麼我們該怎麼用自動屬性呢。

有兩個使用情境

1.在介面中定義,繼承介面的類就可以用了。
2.當你有個欄位不需要限制訪問,你為了編程習慣又不想寫成公有欄位,同時你有懶得主動寫那個欄位名的時候,就使用自動屬性吧。

介面怎麼用自動屬性

在上面的使用情境中介面中是可以聲明自動屬性的,聲明之後意味著繼承介面的所有非抽象的子類都必須實現這個自動屬性,抽象的子類就不用實現了。那麼又不知道產生的欄位叫什麼名字,怎麼實現呢,答案是並沒有辦法具體實現,只能按照介面的寫法照抄一遍,這樣看來介面中用自動屬性並沒有什麼卵用。唯一的使用者就是強制非抽象子類必須有這個屬性,何必呢。在程式二裡發現如果不實現,編譯時間不通過的。得出的結論是介面中還是不要用自動屬性。那麼自動屬性的唯一作用就是在符合編程規範的同時懶一把。

其它文法

介面的方法不允許添加存取修飾詞,預設就是public(類預設是private),不能實現(區別於抽象類別),子類實現時不用override(不同於抽象類別),不能寫欄位;可以寫自動屬性(並沒有什麼卵用,也可能是我知識太淺薄)

程式一
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _31_interface{    class Program    {        static void Main(string[] args)        {            Person p = new Person();            p.Name = "leifeng";            p.Age = 10;            Console.WriteLine("name is {0},age is {1}",p.Name,p.Age);            Console.ReadKey();        }    }    public class Person    {        public string Name        {            set;            get;// { return "SB"; }        }        //Cannot write in this way,because this will cause dead ciculation;in Set and Get,we can not use auto properity;        //so we would better not to fulfill the set and get method of properity;        //so if we need not to limit the get and set method,we can use the auto properity, but why not use the public field;        //So the conclusion is we can only use the auto properity in Interface;        //what interests me is that we can not just fulfill one of the tow method;        public int Age        {            set;            get; { return 20; }        }        private char _gender;        public char Gender        {            get { return _gender; }            set { _gender = value; }        }    }}
程式二:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _31_interface{    public interface IRunable    {         int NumOfLegs        {            set;            get;        }         void Fly();    }    public class Car:IRunable    {        public int NumOfLegs        {            get;            set;        }        public void Fly()        {            Console.WriteLine("Car can run on the road!");        }    }    class Program    {        static void Main(string[] args)        {            Console.ReadKey();        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.