【转】C#运算符重载**

https://www.yiibai.com/csharp/csharp_operator_overloading.html

 

在C#中,可以重新定义或重载大多数内置运算符。 因此,程序员也可以使用具有用户定义类型的运算符。重载运算符是具有特殊名称的功能,关键字operator后跟定义运算符的符号。 类似于任何其他函数定义,重载运算符具有返回类型和参数列表。

例如,通过以下示例函数:

public static Box operator+ (Box b, Box c)
{Box box = new Box();box.length = b.length + c.length;box.breadth = b.breadth + c.breadth;box.height = b.height + c.height;return box;
}

C#

上述函数为用户自定义Box类实现加法运算符(+)。它实现相加两个Box对象的属性,并返回生成的Box对象。

实现操作符重载

以下程序显示了完整的实现:

using System;
namespace OperatorOvlApplication
{class Box{private double length;   // Length of a boxprivate double breadth;  // Breadth of a boxprivate double height;   // Height of a boxpublic double getVolume(){return length * breadth * height;}public void setLength(double len){length = len;}public void setBreadth(double bre){breadth = bre;}public void setHeight(double hei){height = hei;}// Overload + operator to add two Box objects.public static Box operator +(Box b, Box c){Box box = new Box();box.length = b.length + c.length;box.breadth = b.breadth + c.breadth;box.height = b.height + c.height;return box;}}class Tester{static void Main(string[] args){Box Box1 = new Box();   // Declare Box1 of type BoxBox Box2 = new Box();   // Declare Box2 of type BoxBox Box3 = new Box();   // Declare Box3 of type Boxdouble volume = 0.0;    // Store the volume of a box here// box 1 specificationBox1.setLength(60.0);Box1.setBreadth(70.0);Box1.setHeight(50.0);// box 2 specificationBox2.setLength(121.0);Box2.setBreadth(133.0);Box2.setHeight(110.0);// volume of box 1volume = Box1.getVolume();Console.WriteLine("Volume of Box1 : {0}", volume);// volume of box 2volume = Box2.getVolume();Console.WriteLine("Volume of Box2 : {0}", volume);// Add two object as follows:Box3 = Box1 + Box2;// volume of box 3volume = Box3.getVolume();Console.WriteLine("Volume of Box3 : {0}", volume);Console.ReadKey();}}
}

C#

当编译和执行上述代码时,会产生以下结果:

Volume of Box1 : 210000
Volume of Box2 : 1770230
Volume of Box3 : 5878880

Shell

可重载和不可重载的运算符

下表描述了 C# 中重载的运算符:

操作符描述
+, -, !, ~, ++, --这些一元运算符需要一个操作数,并且可以重载。
+, -, *, /, %这些二进制运算符取一个操作数并且可以重载。
==, !=, <, >, <=, >=这些比较运算符都可以重载
&&, //条件逻辑运算符不能直接重载。
+=-=,*=/=%=赋值运算符不能被重载。
=, ., ?:, ->, new, is, sizeof, typeof这些运算符不能重载。

示例

根据上述讨论,我们来扩展上面的例子,并且重载更多的运算符:

using System;
namespace OperatorOvlApplication
{class Box{private double length;    // Length of a boxprivate double breadth;   // Breadth of a boxprivate double height;    // Height of a boxpublic double getVolume(){return length * breadth * height;}public void setLength( double len ){length = len;}public void setBreadth( double bre ){breadth = bre;}public void setHeight( double hei ){height = hei;}// Overload + operator to add two Box objects.public static Box operator+ (Box b, Box c){Box box = new Box();box.length = b.length + c.length;box.breadth = b.breadth + c.breadth;box.height = b.height + c.height;return box;}public static bool operator == (Box lhs, Box rhs){bool status = false;if (lhs.length == rhs.length && lhs.height == rhs.height && lhs.breadth == rhs.breadth){status = true;}return status;}public static bool operator !=(Box lhs, Box rhs){bool status = false;if (lhs.length != rhs.length || lhs.height != rhs.height || lhs.breadth != rhs.breadth){status = true;}return status;}public static bool operator <(Box lhs, Box rhs){bool status = false;if (lhs.length < rhs.length && lhs.height < rhs.height && lhs.breadth < rhs.breadth){status = true;}return status;}public static bool operator >(Box lhs, Box rhs){bool status = false;if (lhs.length > rhs.length && lhs.height > rhs.height && lhs.breadth > rhs.breadth){status = true;}return status;}public static bool operator <=(Box lhs, Box rhs){bool status = false;if (lhs.length <= rhs.length && lhs.height <= rhs.height && lhs.breadth <= rhs.breadth){status = true;}return status;}public static bool operator >=(Box lhs, Box rhs){bool status = false;if (lhs.length >= rhs.length && lhs.height >= rhs.height && lhs.breadth >= rhs.breadth){status = true;}return status;}public override string ToString(){return String.Format("({0}, {1}, {2})", length, breadth, height);}}class Tester{static void Main(string[] args){Box Box1 = new Box();   // Declare Box1 of type BoxBox Box2 = new Box();   // Declare Box2 of type BoxBox Box3 = new Box();   // Declare Box3 of type BoxBox Box4 = new Box();double volume = 0.0;    // Store the volume of a box here// box 1 specificationBox1.setLength(6.0);Box1.setBreadth(7.0);Box1.setHeight(5.0);// box 2 specificationBox2.setLength(12.0);Box2.setBreadth(13.0);Box2.setHeight(10.0);//displaying the Boxes using the overloaded ToString():Console.WriteLine("Box 1: {0}", Box1.ToString());Console.WriteLine("Box 2: {0}", Box2.ToString());// volume of box 1volume = Box1.getVolume();Console.WriteLine("Volume of Box1 : {0}", volume);// volume of box 2volume = Box2.getVolume();Console.WriteLine("Volume of Box2 : {0}", volume);// Add two object as follows:Box3 = Box1 + Box2;Console.WriteLine("Box 3: {0}", Box3.ToString());// volume of box 3volume = Box3.getVolume();Console.WriteLine("Volume of Box3 : {0}", volume);//comparing the boxesif (Box1 > Box2)Console.WriteLine("Box1 is greater than Box2");elseConsole.WriteLine("Box1 is  greater than Box2");if (Box1 < Box2)Console.WriteLine("Box1 is less than Box2");elseConsole.WriteLine("Box1 is not less than Box2");if (Box1 >= Box2)Console.WriteLine("Box1 is greater or equal to Box2");elseConsole.WriteLine("Box1 is not greater or equal to Box2");if (Box1 <= Box2)Console.WriteLine("Box1 is less or equal to Box2");elseConsole.WriteLine("Box1 is not less or equal to Box2");if (Box1 != Box2)Console.WriteLine("Box1 is not equal to Box2");elseConsole.WriteLine("Box1 is not greater or equal to Box2");Box4 = Box3;if (Box3 == Box4)Console.WriteLine("Box3 is equal to Box4");elseConsole.WriteLine("Box3 is not equal to Box4");Console.ReadKey();}}
}

C#

当编译和执行上述代码时,会产生以下结果:

Box 1: (6, 7, 5)
Box 2: (12, 13, 10)
Volume of Box1 : 210
Volume of Box2 : 1560
Box 3: (18, 20, 15)
Volume of Box3 : 5400
Box1 is not greater than Box2
Box1 is less than Box2
Box1 is not greater or equal to Box2
Box1 is less or equal to Box2
Box1 is not equal to Box2
Box3 is equal to Box4

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/csharp/csharp_operator_overloading.html
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/436654.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图形工具包 linux,GTK 4.0图形工具包正式发布:时隔四年的重大版本!

GTK是用于创建图形用户界面的工具包&#xff0c;GTK提供了一整套的小部件&#xff0c;适用于从小型一次性工具到完整的应用程序套件的项目。GTK是GNOME开发平台的核心&#xff0c;但是它也可以用于编写其他Linux环境的应用程序&#xff0c;以及针对微软Windows和苹果macOS的应用…

引用到网站绝对路径Server.MapPath(~/myfile.mdb)

在任何路径下&#xff0c;都可以用Server.MapPath("~/")引用到网站根目录下 转载于:https://www.cnblogs.com/yurichou/archive/2005/10/17/256357.html

【转】继承过程中 父类子类的 字段方法 内存分配 (非java语言)

名人名言&#xff1a;思想好比火星&#xff1a;一颗火星会点燃另一颗火星。一个深思熟虑的教师和班主任&#xff0c;总是力求在集体中创造一种共同热爱科学和渴求知识的气氛&#xff0c;使智力兴趣成为一些线索&#xff0c;以其真挚的、复杂的关系——即思想的相互关系把一个个…

linux设备驱动学习,linux设备驱动学习4

Linux设备驱动程序学习(4)-高级字符驱动程序操作&#xff3b;(1)ioctl and llseek&#xff3d;今天进入《Linux设备驱动程序(第3版)》第六章高级字符驱动程序操作的学习。一、ioctl大部分设备除了读写能力&#xff0c;还可进行超出简单的数据传输之外的操作&#xff0c;所以设备…

几个删除重复记录的SQL语句

几个删除重复记录的SQL语句在大的数据库应用中&#xff0c;经常因为各种原因遇到重复的记录&#xff0c;造成数据的冗余和维护上的不便。1.用rowid方法2.用group by方法3.用distinct方法 1。用rowid方法据据oracle带的rowid属性&#xff0c;进行判断&#xff0c;是否存在重复,语…

【转】产品经理如何进行BRD,MRD,PRD,DRD,FRD编写

转载自&#xff1a;http://minjiechenjava.iteye.com/blog/2304490&#xff0c; 侵删 PRD文档即产品需求文档&#xff0c;也叫业务需求文档。是产品项目由“概念化”阶段进入到“图纸化”阶段的最主要的一个文档。 产品需求文档的作用就是“对MRD中的内容进行指标化和技术化”…

linux磁盘永久挂载教程,linux 永久磁盘挂载

包年包月实例过期后&#xff0c;如果未在规定时间内续费&#xff0c;实例和磁盘均会自动释放&#xff0c;数据永久丢失&#xff0c;无法找回。关于预付费资源过期后的状态变化&#xff0c;请参考 预付费(包年包月)。在使用包年包月实例过程中&#xff0c;如果您觉得当前实例配置…

[代码阅读] ECS toString实现方法

引言 ECS 提供了一种编程方式来生成以不同标记语言编写的文档。它设计为通过面向对象的抽象来生成所有标签。 ECS 目前版本为1.4.2 &#xff0c;支持 HTML 4.0 和 XML 。 因为工作原因&#xff0c;作者粗略读了ECS的部分原代码&#xff0c;着重了解ECS如果通过toString方法…

【转】设备数据通过Azure Functions 推送到 Power BI 数据大屏进行展示

设备数据通过Azure Functions 推送到 Power BI 数据大屏进行展示&#xff08;1.准备工作&#xff09; 原创 Sean Yu 云计算实战 2019-12-06 本案例适用于开发者入门理解Azure Functions/ IoT Hub / Service Bus / Power BI等几款产品。 主要实战的内容为&#xff1a; 将设备遥…

linux系统管理Linux系统实验,实验三 linux系统管理

实验三 linux系统管理【实验目的】练习Linux系统管理&#xff0c;熟悉Linux系统管理。【实验要求】通过Liunx用户和组管理、设备管理、文件系统管理、进程管理和shell程序设计&#xff0c;能够掌握linux系统管理&#xff0c;完成系统日常维护和管理工作&#xff0c;最后上交实验…

【转】Azure Messaging-ServiceBus Messaging消息队列技术系列1-基本概念和架构

前段时间研究了Window Azure ServiceBus Messaging消息队列技术&#xff0c;搞了很多技术研究和代码验证&#xff0c;最近准备总结一下&#xff0c;分享给大家。 首先&#xff0c;Windows Azure提供了两种类型的消息队列机制&#xff1a;Azure Queues和ServiceBus Queues。 其…

Atlas应用程序调试技巧

本文为翻译文章&#xff0c;原文地址&#xff1a;http://atlas.asp.net/docs/Overview/debug.aspx “Atlas”程序由服务器端代码和客户端代码组成&#xff0c;并且&#xff0c;浏览器可能会要去异步请求一些数据。那么&#xff0c;怎样才能Debug这样的web程序呢。本文将告诉…

linux 自动连接无限,hotplug应用实例:自动连接无线网

Linux内核提供了一种机制&#xff0c;使得有热插拔事件(比如插入或拔出U盘)发生时可以执行一个程序&#xff0c;在本文中我称之为hotplug程序。内核在调用hotplug程序时会传递一个命令行参数&#xff0c;这个参数是发生热插拔事件的子系统名称&#xff0c;常见的有usb, module,…

网络数据库的复制和同步(转摘)

数据库复本--网络数据库的复制和同步&#xff08;1&#xff09; 数据库复本是复制数据库的技术,利用这种技术,可以是数据库的几个拷贝保持同步。数据库的每个拷贝称为一个复本&#xff0c;并且每个复本都包含一个公共的表、查询、窗体、报表、宏和模块的集合&#xff1b;每个复…

【转】Azure Messaging-ServiceBus Messaging消息队列技术系列2-编程SDK入门

各位&#xff0c;上一篇基本概念和架构中&#xff0c;我们介绍了Window Azure ServiceBus的消息队列技术的概览。接下来&#xff0c;我们进入编程模式和详细功能介绍模式&#xff0c;一点一点把ServiceBus技术研究出来。 本章我们主要介绍ServiceBus的编程SDK编程入门。 首先…

linux octave源码安装,在Linux操作系统上安装Octave的方法

本文介绍在Ubuntu、Debian、Arch Linux、Fedora、OpenSUSE操作系统上安装Octave(也称GNU Octave)的方法&#xff0c;它还支持Flatpak方式安装。简介GNU Octave是一种用于科学和数学计算操作的解释性命令语言&#xff0c;它与Matlab兼容&#xff0c;支持各种扩展&#xff0c;允许…

C#做的一个加密/解密的类

C#做的一个加密/解密的类 大家要有兴趣&#xff0c;可以一起来讨论一下 WebService数据交互安全问题&#xff0c;以下的这个代码&#xff0c;可以用于Dotnet环境下的任何托管方式的应用程序&#xff0c;在实际应用中有两个实例。其中&#xff0c;有一个挂在Internet上的&#x…

【转】理解Azure订阅,账户,活动目录AD,租户等概念

理解Azure订阅&#xff0c;账户&#xff0c;活动目录AD&#xff0c;租户等概念 订阅是啥&#xff1f; 活动目录AD Active Directory啥是租户&#xff1f;订阅是啥&#xff1f; “An Azure subscription is a logical unit of Azure services that links to an Azure account,…

linux安装静默安装was7,WAS7.0 - 安装并升级WAS7.0.0.31(静默安装)

WAS7.0 - 安装并升级WAS7.0.0.31(静默安装)安装之前需要准备的文件&#xff1a;C1G35ML.tar.gz&#xff1a;WAS7.0安装包C1G36ML.tar.gz&#xff1a;WAS升级工具7.0.0-WS-WASSDK-LinuxX64-FP0000031.pak 7.0.0-WS-WAS-LinuxX64-FP0000031.pak &#xff1a;7.0.0.31升级包&…

Vb.Net实现图片合并(相框效果)

Vb.Net实现图片合并(相框效果) Imports System.DrawingImports System.Drawing.ImagingImports System.Drawing.Drawing2D Public Class ImgMerg Public Shared Function MergedImage(ByVal innerImgPath As String, ByVal outerImgPath As String, ByVal mergImgPath As St…