c#屬性(Property)


屬性(Property)是類(class)、結構(structure)和接口(interface)的命名(named)成員。類或結構中的成員變量或方法稱為 域(Field)。屬性(Property)是域(Field)的擴展,且可使用相同的語法來訪問。它們使用 訪問器(accessors) 讓私有域的值可被讀寫或操作。

該代碼主要是幫助讀者了解屬性的用法,代碼實現了添加屬性值和根據添加的屬性值進行篩選

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace 編碼練習
{
    //創建一個類包含兩個屬性
    class People
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public People(string name, string address)
        {
            this.Name = name;
            this.Address = address;
        }

    }
    //繼承兩個接口(枚舉/格式化)
    class Peoples : IEnumerable
    {
        //創建一個對象列表
        private List<People> Lpeoples { get; set; }
        private StringBuilder Sbuilder { get; set; }
        public Peoples()
        {
            Lpeoples = new List<People>();
        }
        //創建一個可以往列表加屬性實例的方法
        public void Add(People people)
        {
            Lpeoples.Add(people);
        }
        //獲取對象值
        public IEnumerator GetEnumerator()
        {
            foreach (var p in Lpeoples)
            {
                yield return p;
            }
        }

        public override string ToString()
        {
            return GetContent(Lpeoples);
        }
        public string Tostring(string format)
        {
            return ToString(format, CultureInfo.CreateSpecificCulture("zh-CN"));
        }
        public string ToString(string format, IFormatProvider formatProvider)
        {
            IEnumerable<People> ps = Lpeoples;
            if (format.ToUpperInvariant() == "B")
            {
                ps = from p in Lpeoples where p.Address == "北京" select p;
            }
            else if (format.ToUpperInvariant() == "S")
            {
                ps = from p in Lpeoples where p.Address == "上海" select p;
            }
            return GetContent(ps);
        }
        //將數據連接到數組
        private string GetContent(IEnumerable<People> peoples)
        {
            Sbuilder = new StringBuilder();
            foreach (var p in peoples)
            {
                Sbuilder.AppendLine(string.Format("{0}{1}", p.Name, p.Address));
            }
            return Sbuilder.ToString();
        }
    }
    public class Start
    {
        public static void Main()
        {
            Peoples peoples = new Peoples()
            {new People("zhangsan","北京"),new People("lisi","上海"),new People("wangwu","北京"),new People("naliu","北京")};
            Console.WriteLine("本站會員有:");
            Console.WriteLine(peoples.ToString());
            Console.WriteLine("北京的會員有:");
            Console.WriteLine(peoples.Tostring("B"));
            Console.WriteLine("上海的會員有:");
            Console.WriteLine(peoples.Tostring("S"));
            Console.ReadLine();

        }
    }
}

附加知識:

  IFormatProvider接口的引用,該接口用於格式化操作,當進行數字,日期時間,字符串匹配時,都會進行CultureInfo的操作,文中CultureInfo.CreateSpecificCulture("zh-CN")意為結果輸出的是<簡體,中國>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM