技术控

    今日:85| 主题:49531
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] C# – Operator Overloading

[复制链接]
一厘米的距离ゃ 发表于 2016-10-2 03:31:47
270 6

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
In this article, we will discuss about operator overloading in C#.
  Background:

  Operator overloading is very Like Method Overloading. All operator Overloading methods are static methods of the class. In this article we will calculate area of two squares using Operator Overloading. Operator will be used between objects of class. Here, I am going to use console Application of visual studio.
  The Complete list of Operators that can be overloaded are:

  
       
  • Unary Operator : +,-,!,~,++,–,true,false   
  • Binary Operator :+,-,*,/,%,&,|,^,<<,>>,==,!=,>,<,>=,<=  
  The operator which cannot be overloaded

  =,&&,||,?:,Cheeked, new, typeof, as, and is operator.
  Now we will create Square class. Below is the following codes.

  [code]public class Square
    {
        public decimal Length { set; get; }
        public decimal Width { set; get; }

        public static Squareoperator *(Square_squareObj1, Square_squareObj2)
        {
            Square_squareObj = new Square();
            _squareObj.Length = _squareObj1.Length * _squareObj2.Length;
            _squareObj.Width = _squareObj1.Width * _squareObj2.Width;
            return _squareObj;
        }


        public decimal Area()
        {
            return Length * Width;
        }
    }
[/code]  Let’s get explanation about the code:

  [code]public class Square
[/code]  Here, square is the class name.

  [code]public decimal Length { set; get; }
public decimal Width { set; get; }
[/code]  There are two property of Square Class. One is Length which has decimal data type. And other is width which has decimal type.
  [code]        public static Squareoperator *(Square_squareObj1, Square_squareObj2)
        {
            Square_squareObj = new Square();
            _squareObj.Length = _squareObj1.Length * _squareObj2.Length;
            _squareObj.Width = _squareObj1.Width * _squareObj2.Width;
            return _squareObj;
        }
[/code]  The above method implements the Multiplication operator (*) for a user-defined class Square. This method is a static method which have tow parameter. Both are Square’s class objects and both objects done multiplication.
  [code]public decimal Area()
{
    return Length * Width;
}
[/code]  We have Multiplication Length and Width.
  Now we will call Square class in Main function:

  [code]class Program
    {
        static void Main(string[] args)
        {
            // First Square
            Square_obj1 = new Square();
            _obj1.Length = 40;
            _obj1.Width = 40;

            // Second Square
            Square_obj2 = new Square();
            _obj2.Length = 50;
            _obj2.Width = 50;

            // For match tow Object
            Square_obj3 = new Square();
            _obj3 = _obj1 * _obj2;

            // Assign area to variable
            decimal area = _obj3.Area();
            Console.WriteLine(area);
            Console.ReadKey();

        }
    }
[/code]  Let’s get explanation about the code:

  We have calculated total area of two squares.
  [code] Square_obj1 = new Square();
_obj1.Length = 40;
_obj1.Width = 40;
[/code]  We have create object which name given _obje1. We have assigned length of square 40 and  width of square 40
  [code]Square_obj2 = new Square();
_obj2.Length = 50;
_obj2.Width = 50;
[/code]  Again, we have create object which name given _obje2. We have assigned length of square 50 and other width of square 50.
  [code]Square_obj3 = new Square();
_obj3 = _obj1 * _obj2;
[/code]  New Object have been created for multiplication two object.
  [code]decimal area = _obj3.Area();
Console.WriteLine(area);
Console.ReadKey();
[/code]  The Area method have been called and assigned result to area variable.
   
C# – Operator Overloading-1 (following,discuss,article,between,methods)

  Hope this will be helpful ��
友荐云推荐




上一篇:Learned helplessness and the languages of DAO
下一篇:Two Arduinos Become One
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

方力眠n 发表于 2016-10-2 05:58:51
方力眠n涨姿势了
回复 支持 反对

使用道具 举报

haining770 发表于 2016-10-2 06:22:43
锻炼肌肉,防止挨揍!  
回复 支持 反对

使用道具 举报

redak 发表于 2016-10-2 06:53:02
别回头,哥恋的只是你的背影。
回复 支持 反对

使用道具 举报

雅柔 发表于 2016-10-2 11:45:58
友情提示:由于此用户的签名太过于个性而被系统自动屏蔽
回复 支持 反对

使用道具 举报

66644383 发表于 2016-11-7 10:49:53
撸过。。。。
回复 支持 反对

使用道具 举报

new5201314 发表于 2016-11-11 13:32:56
不作死就不会死
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表