C#語法學習IComparable接口

/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/9/3
 * Time: 下午 04:52
 * 接口的應用IComparable
 * 通常小的應用程序中不會應用到接口
 * 在一個大的系統中會應用的比較多.
 * 因為有了接口,設計人員只需要關注接口,
 * 而不用去關注其實現.
 * 也就出現了現在面向接口編程
 * 此外在設計模式中也大量使用接口
 
*/
using  System;
class  Student:IComparable
{
    
private   string  _name;
    
private   int  _age;
    
public  Student( string  name, int  age)
    {
        
this ._name = name;
        
this ._age = age;
    }
    
public   string  Name
    {
        
get  {  return   this ._name; }
        
set  {  this ._name = value; }
    }
    
public   int  Age
    {
        
get  {  return   this ._age;}
        
set  {  this ._age = value; }
    }
    
public   int  CompareTo( object  right)
    {
        
if ( ! (right  is  Student))
        {
            
throw   new  ArgumentException( " 參數必須為Student類型 " );
        }
        
return   this ._name.CompareTo(((Student)right)._name);
    }
    
public   override   string  ToString()
    {
        
return  _name;
    }
}
class  Test
{
    
static   void  Main()
    {
        Student[] arr
= new  Student[ 5 ];
        arr[
0 ] = new  Student( " 張三 " , 5 );
        arr[
1 ] = new  Student( " 李四 " , 3 );
        arr[
2 ] = new  Student( " 王五 " , 2 );
        arr[
3 ] = new  Student( " 馬六 " , 4 );
        arr[
4 ] = new  Student( " 錢七 " , 1 );
        Array.Sort(arr);
        
foreach (Student i  in  arr)
        {
            Console.WriteLine(i.Name 
+   "    "   +  i.Age);
            
// Console.WriteLine(i.ToString());
        }
    }
}

/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/9/3
 * Time: 下午 04:52
 * 用接口實現排序
 * 實現IComparable接口時,我們應該使用顯式接口
 * 實現,同時為接口中的方法提供一個公有的強類
 * 型重載版本.強類型的重載版本在提高代碼性能
 * 的同時,能減少誤用CompareTo()方法的可能.
 
*/
using  System;
using  System.Collections;
class  Student:IComparable
{
    
private   string  _name;
    
private   int  _age;
    
public  Student( string  name, int  age)
    {
        
this ._name = name;
        
this ._age = age;
    }
    
public   string  Name
    {
        
get  {  return   this ._name; }
        
set  {  this ._name = value; }
    }
    
public   int  Age
    {
        
get  {  return   this ._age;}
        
set  {  this ._age = value; }
    }
    
// 顯式調用
     int  IComparable.CompareTo( object  right)
    {
        
if ( ! (right  is  Student))
        {
            
throw   new  ArgumentException( " 參數必須為Student類型 " );
        }
        
return   this ._name.CompareTo(((Student)right)._name);
    }
    
// 公有的強類型重載版本
     public   int  CompareTo(Student right)
    {
        
return   this ._name.CompareTo(right._name);
    }
    
// 保證只創建一次AgeComparer只會產生一個實例
     private   static  AgeComparer _ageCom = null ;
    
public   static  IComparer AgeCom
    {
        
get
        {
            
if (_ageCom == null )
            {
                _ageCom
= new  AgeComparer();
            }
            
return  _ageCom;
        }
    }
    
private   class  AgeComparer:IComparer
    {
        
int  IComparer.Compare( object  left, object  right)
        {
            
if ( ! (left  is  Student)  ||   ! (right  is  Student))
            {
                
throw   new  ArgumentException( " 參數必須為Student類型 " );
            }
            
return  (((Student)left)._age.CompareTo(((Student)right)._age));
        }
    }
    
public   override   string  ToString()
    {
        
return  _name;
    }
}
class  Test
{
    
static   void  Main()
    {
        Student[] arr
= new  Student[ 5 ];
        arr[
0 ] = new  Student( " 張三 " , 5 );
        arr[
1 ] = new  Student( " 李四 " , 3 );
        arr[
2 ] = new  Student( " 王五 " , 2 );
        arr[
3 ] = new  Student( " 馬六 " , 4 );
        arr[
4 ] = new  Student( " 錢七 " , 1 );
        Console.WriteLine(
" 對姓名進行排序 " );
        Array.Sort(arr);
        
foreach (Student i  in  arr)
        {
            Console.WriteLine(i.Name 
+   "    "   +  i.Age);
            
// Console.WriteLine(i.ToString());
        }
        Console.WriteLine(
" 對年齡進行排序 " );
        Array.Sort(arr,Student.AgeCom);
        
foreach (Student i  in  arr)
        {
            Console.WriteLine(i.Name 
+   "    "   +  i.Age);
            
// Console.WriteLine(i.ToString());
        }        
    }
}

你可能感兴趣的:(comparable)