.NET中的常用接口

一 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);
        }


        
implement IComparable interface
    }

 三 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);
        }


        
implement IComparable  generic interface

四 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);
        }

        
implement IEquatable  interface
    }

五  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));
        }


        
implement IFormattable interface
    }

六 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

  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();
            
//demo.Dispose();
            use using keyword
        }

    }

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

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

        
public void Dispose()
        
{
            Console.WriteLine(
"Dispose is called.");
            
GC.SuppressFinalize
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值