C# 靜態類和非靜態類(執行個體類)

來源:互聯網
上載者:User

標籤:工具類   one   alt   name   lap   bsp   ide   log   src   

1.非靜態類裡面可以出現靜態成員和非靜態成員

using System;namespace shuzu{        class dom_class    {        public string name = "張三";        private int age;        public int Age//通過屬性訪問私人成員        {            get { return age; }            set { age = value; }        }    }    class aclass {        static void Main(string[] args)        {            dom_class s = new dom_class();            s.name = "abc";            s.Age = 15;            Console.WriteLine(s.name);             Console.WriteLine(s.Age);            Console.ReadKey();        }    }}
View Code

2.靜態類中只能出現靜態成員,不允許建立對象

using System;namespace shuzu{     static class aclass {     static int i = 15;        static void Main(string[] args)     {         i = 13;//訪問靜態成員            Console.WriteLine(aclass.i);//也可以這樣訪問            Console.WriteLine(i);            Console.ReadKey();        }    }}
View Code

3.非靜態成員和靜態成員調用上的區別   (類名點對象名)非靜態(對象名點成員名)

using System;namespace shuzu{        class dom_class    {        public static int shu = 13; //靜態成員的訪問    }    class aclass {        static void Main(string[] args)        {           int g= dom_class.shu = 15;//類名。對象名訪問             Console.WriteLine(g);            Console.ReadKey();        }    }}
View Code
using System;namespace shuzu{        class dom_class    {        public string name = "張三";//非靜態成員的訪問        private int age;        public int Age//通過屬性訪問私人成員        {            get { return age; }            set { age = value; }        }    }    class aclass {        static void Main(string[] args)        {            dom_class s = new dom_class();            s.name = "abc";//通過建立對象訪問            s.Age = 15;            Console.WriteLine(s.name);             Console.WriteLine(s.Age);            Console.ReadKey();        }    }}
View Code

4.什麼時候使用靜態類?  當作為工具類使用的時候使用靜態類

C# 靜態類和非靜態類(執行個體類)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.