laudy的博客

人变聪明容易,但想装糊涂可真难
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.NET中的常用接口

Posted on 2007-12-12 16:37  laudy  阅读(560)  评论(0)    收藏  举报
一 ICloneable 

说明:浅拷贝指仅拷贝值类型,深拷贝指拷贝值类型和引用类型

class Program
{
        
static void Main(string[] args)
        {
            Employee emp1 
= new Employee(128"Tim");
            emp1.HomeAddress 
= new Address("Guangdong""Guangzhou""Haizhu");
            Employee emp2 
= (Employee)emp1.Clone();
           
            emp1.ShowInfo();
            emp2.ShowInfo();
            emp2.WorkNum 
= 200;
            emp2.Name 
= "Snake";
            emp2.HomeAddress.Street 
= "Tianhe";
            emp1.ShowInfo();
            emp2.ShowInfo();
            Console.ReadLine();
        }

}

class Employee : ICloneable
{
        
public int WorkNum;
        
public string Name;
        
public Address HomeAddress;
        
public Employee(int workNum, string name)
        
{
            
this.WorkNum = workNum;
            
this.Name = name;   
        }

        
public void ShowInfo()
        
{
            Console.WriteLine(
"WorkNum:{0} Name:{1} HomeAddres:{2} "this.WorkNum, this.Name, this.HomeAddress);
        }

        
// shallow cloning
        
//public object Clone()
        
//{
        
//    Employee emp = new Employee(this.WorkNum, this.Name);
        
//    emp.HomeAddress = this.HomeAddress;
        
//    return emp;
        
//}
        
// deep cloning
        public object Clone()
        
{
            Employee emp 
= new Employee(this.WorkNum, this.Name);
            emp.HomeAddress 
= new Address(this.HomeAddress.Province, this.HomeAddress.City, this.HomeAddress.Street);
            
return emp;
        }

    }

    
class Address
    
{
        
public string Province;
        
public string City;
        
public string Street;
        
public Address(string province, string city, string street)
        
{
            
this.Province = province;
            
this.City = city;
            
this.Street = street;
        }

        
public override string ToString()
        
{
            
return string.Format("{0} {1} {2}"this.Province, this.City, this.Street);
        }

    }

二 ICompareable

 class Program
    
{
        
static void Main(string[] args)
        
{
            Complex num1 
= new Complex(23);
            Complex num2 
= new Complex(32);
            Console.WriteLine(
"num1 = {0}", num1.ToString());
            Console.WriteLine(
"num2 = {0}", num2.ToString());
            
try
           
{
                
//Console.WriteLine("num1 CompareTo num2 ? {0}", num1.CompareTo(num2));
                Console.WriteLine("num1 CompareTo num2 ? {0}", num1.CompareTo("haha"));
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }

    }

    
class Complex : IComparable
    
{
        
public int real;
        
public int imaginary;
        
public Complex(int real, int imaginary)
        
{
            
this.real = real;
            
this.imaginary = imaginary;
        }

        
public double Module()
        
{
            
return Math.Sqrt(Math.Pow(this.real, 2+ Math.Pow(this.imaginary, 2));
        }

        
public override string ToString()
        
{
            
return string.Format("{0} + {1}i"this.real, this.imaginary);
        }


        
#region implement IComparable interface
        
public int CompareTo(object obj)
        
{
            
if (obj is Complex)
            
{
                Complex other 
= (Complex)obj;
                
return this.Module().CompareTo(other.Module());
            }

            
throw new ArgumentException("Object is not an Complex.");
        }

        
#endregion

    }

三 ICompareableGeneric

 class Program
    
{
        
static void Main(string[] args)
        
{
            Complex num1 
= new Complex(23);
            Complex num2 
= new Complex(32);
            Complex num3 
= null;
            Console.WriteLine(
"num1 = {0}", num1.ToString());
            Console.WriteLine(
"num2 = {0}", num2.ToString());
            
try
            
{
                
//Console.WriteLine("num1 CompareTo num2 ? {0}", num1.CompareTo(num2));
                Console.WriteLine("num1 CompareTo num2 ? {0}", num1.CompareTo(num3));
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }

    }

    
class Complex : IComparable<Complex>
    
{
        
public int real;
        
public int imaginary;
        
public Complex(int real, int imaginary)
        
{
            
this.real = real;
            
this.imaginary = imaginary;
        }

        
public double Module()
        
{
            
return Math.Sqrt(Math.Pow(this.real, 2+ Math.Pow(this.imaginary, 2));
        }

        
public override string ToString()
        
{
            
return string.Format("{0} + {1}i"this.real, this.imaginary);
        }


       
#region implement IComparable<Complex> generic interface
        
public int CompareTo(Complex other)
        
{
            
if (other == nullthrow new NullReferenceException();
            
return this.Module().CompareTo(other.Module());
        }

        
#endregion

四 IEqualable 

 class Program
    
{
        
static void Main(string[] args)
        
{
            Complex num1 
= new Complex(23);
            Complex num2 
= new Complex(23);
            Complex num3 
= null;
            Console.WriteLine(
"num1 = {0}", num1.ToString());
            Console.WriteLine(
"num2 = {0}", num2.ToString());
            
try
            
{
                Console.WriteLine(
"num1 Equals num2 ? {0}", num1.Equals(num2));
                Console.WriteLine(
"num1 Equals num2 ? {0}", num1.Equals("haha"));
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }

    }

    
class Complex : IEquatable<Complex>
    
{
        
public int real;
        
public int imaginary;
        
public Complex(int real, int imaginary)
       
{
            
this.real = real;
            
this.imaginary = imaginary;
        }

        
public double Module()
        
{
            
return Math.Sqrt(Math.Pow(this.real, 2+ Math.Pow(this.imaginary, 2));
        }

        
public override string ToString()
        
{
            
return string.Format("{0} + {1}i"this.real, this.imaginary);
        }

       
#region implement IEquatable<Complex> interface
        
public override bool Equals(object other)
        
{
            
return Equals(other as Complex);
        }

        
public bool Equals(Complex other)
        
{
            
if (other == nullreturn false;
            
return (this.real == other.real && this.imaginary == other.imaginary);
        }

        
#endregion

    }

五  IFormattable 

 class Program
    
{
        
static void Main(string[] args)
        
{
            Complex num1 
= new Complex(23);
            
try
            
{
                Console.WriteLine(
"num1 = {0}", num1);
                Console.WriteLine(
"num1 = {0:N}", num1);
                Console.WriteLine(
"num1 = {0:X}", num1);
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }

    }

    
class Complex : IFormattable
   
{
        
public int real;
        
public int imaginary;
        
public Complex(int real, int imaginary)
        
{
            
this.real = real;
            
this.imaginary = imaginary;
        }

        
public double Module()
        
{
            
return Math.Sqrt(Math.Pow(this.real, 2+ Math.Pow(this.imaginary, 2));
        }


        
#region implement IFormattable interface
       
public override string ToString() return ToString(nullnull); }
        
public string ToString(string format, IFormatProvider fp)
        
{
            
// If no format is passed, display like this: x + yi
            if (format == nullreturn string.Format("{0} + {1}i"this.real, this.imaginary);
            
// For "N" formatting, display like this: (x,y)
            if (format == "N"return string.Format("({0},{1})"this.real, this.imaginary);
           
// For any unrecognized format, throw an exception.
            throw new FormatException(string.Format("Invalid format string: '{0}'.", format));
        }

        
#endregion

    }

六 Destructor 

 class Program
    
{
        
static void Main(string[] args)
        
{
            Create();
            Console.WriteLine(
"After DemoClass created.");
            Console.ReadLine();
            GC.Collect();
            Console.ReadLine();
        }

        
static void Create()
        
{
            DemoClass demo 
= new DemoClass();
        }

    }

    
class DemoClass
    
{
        
public DemoClass()
        
{
            Console.WriteLine(
"Constructor is called.");
        }

        
~DemoClass()
        
{
            Console.WriteLine(
"Destructor is called.");
        }

    }

IDisposeable

见:正确使用IDisposable