CLR via C#学习笔记:C#操作符重载学习( 基于.NET3.5 )

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

namespace  ConsoleApplication1
{
    
class MyCls
    
{
        
public int X
        
{
            
get;
            
set;
        }

        
public int Y
        
{
            
get;
            
set;
        }

        
public override string ToString()
        
{
            
return string.Format("X={0},Y={1}", X, Y);
        }

        
public override bool Equals(object obj)
        
{
            MyCls a 
= obj as MyCls;

            
return a.X == this.X && a.Y == this.Y;
        }

        
public override int GetHashCode()
        
{
            
return X * Y;
        }
      
        
public static MyCls operator +(MyCls a, MyCls b)
        
{
            
return new MyCls() { X = a.X + b.X, Y = a.Y + b.Y };
        }

        
public static MyCls operator -(MyCls a, MyCls b)
        
{
            
return new MyCls { X = a.X - b.X, Y = a.Y - b.Y };
        }

        
public static MyCls operator ++(MyCls a)
        
{
            
return new MyCls() { X = a.X++, Y = a.Y++ };
        }

        
public static MyCls operator --(MyCls a)
        
{
            
return new MyCls() { X = a.X--, Y = a.Y-- };
        }

        
public static bool operator ==(MyCls a, MyCls b)
        
{
            
return a.X == b.X && a.Y == b.Y;
        }

        
public static bool operator !=(MyCls a, MyCls b)
        
{
            
return a.X != b.X && a.Y != b.Y;
        }

        
public static void Main()
        
{
            MyCls a 
= new MyCls { X = 1, Y = 1 };
            MyCls b 
= new MyCls { X = 2, Y = 2 };
            Console.WriteLine(a 
+ b);
            Console.WriteLine(b 
- a);
            Console.WriteLine(b
++);
            Console.WriteLine(a
--);
            Console.WriteLine(a
++ == b);
            Console.WriteLine(a
!= b--);
            Console.ReadLine();
        }

    }

}


 

输出结果:

X=3,Y=3
X=1,Y=1
X=3,Y=3
X=0,Y=0
True
False

 

posted on 2008-05-25 13:24  阿牛-专注金融行业开发 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/rockniu/archive/2008/05/25/1206880.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值