(200-06-28) C#.NET 實作IComparable<T>介面

摘要:(200-06-28) C#.NET 實作IComparable介面

List 排序方式 實作 IComparable<T> 介面

父類別 Employee

 

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

namespace csmod01.khh.domain
{
    /// <summary>
    /// 員工類別
    /// </summary>
    public class Employee
    {
        #region "資料欄與屬性"
        //Data Field
        private String _id;
        /// <summary>
        /// 設定與取得員工編號
        /// </summary>
        public String Id
        {
            get { return _id; }
            set { 
                _id = value; 
            }
        }
        private String _name;
        /// <summary>
        /// 設定獲取得員工姓名
        /// </summary>
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private String _address;

        public String Address
        {
            get { return _address; }
            set { _address = value; }
        }
        private DateTime _birthDate;

        public DateTime BirthDate
        {
            get { return _birthDate; }
            set 
            {
                if (value < DateTime.Now)
                {
                    _birthDate = value;
                }
                
            }
        }
        #endregion 
        //常數const 預設static
        public const String COMPANYNAME = "巨匠";

        //建構子
        /// <summary>
        /// 空參數建構子 使用存取子設定資訊
        /// </summary>
        public Employee() { }
        //建構子多載
        /// <summary>
        /// 具有起始資訊建構子
        /// </summary>
        /// <param name="_id">編號</param>
        /// <param name="_name">姓名</param>
        public Employee(String _id, String _name)
        {
            this._id = _id;
            this._name = _name;
        }
    }
}

子類別 Salse

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using csmod01.khh.domain;

namespace csmod01
{
    //單一繼承Employee
    //實作介面Interface ,多重實做
   public class Sales:Employee,IComparable<Sales>
    {
       //Data Field
        private Decimal _qa;
        private DateTime _bir;

        public DateTime Bir
        {
            get { return _bir; }
            set { _bir = value; }
        }

        public Decimal Qa
        {
            get { return _qa; }
            set {
                if (value > 0)
                {
                    _qa = value;
                }
            }
        }
       

        #region IComparable<Sales> 成員

        public int CompareTo(Sales other)
        {
            Int32 r = 0; //相等
            //比對
            if (this._qa > other._qa)
            {
                r = -1;
            }
            if (this._qa < other._qa)
            {
                r = 1;
            }   
            return r;
         }

        #endregion

        public Sales():base()
        {
        }
   

        public Sales(String _id, String _name): base(_id, _name)
        {
            
        }
    }
}

主程式

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using csmod01.khh.domain;
namespace csmod01
{
    class TestSales
    {
        //主程式
        public static void Main()
        {
            //建構泛型集合物件 收集員工類型
            System.Collections.Generic.List<Sales> col = 
                new List<Sales>();
            Sales s1=new Sales();
            s1.Qa=10000;
            s1.Bir = new DateTime(2010, 5, 10);
            col.Add(s1);
            Sales s2 = new Sales();
            s2.Qa = 50000;
            s2.Bir = new DateTime(2010, 7, 10);
            col.Add(s2);
            Sales s3 = new Sales();
            s3.Qa = 20000;
            s3.Bir = new DateTime(2010, 6, 10);
            col.Add(s3);
            foreach (Sales  e in col)
            {
                //if (e is Sales)
                //{
                //    //強制轉型
                //    Sales s = (Sales)e;
                System.Console.WriteLine(e.Bir.ToString("yyyy-MM-dd"));
              }
                     //逐一參考
            col.Sort();
                foreach (Sales  e in col)
            {
                //if (e is Sales)
                //{
                //    //強制轉型
                //    Sales s = (Sales)e;
                    System.Console.WriteLine(e.Bir.ToString("yyyy-MM-dd"));
                }
            }
        }
    }