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 Lpeoples { get; set; }
        private StringBuilder Sbuilder { get; set; }
        public Peoples()
        {
            Lpeoples = new List();
        }
        //创建一个可以往列表加属性实例的方法
        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 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 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")意为结果输出的是<简体,中国>

 

你可能感兴趣的:(c#属性(Property))