C#运算符重载
c# 运算符 重载
2023-09-11 14:16:46 时间
一 运算符
1 使用运算符的例子
① this.Location+=new Size(10,10);
② TimeSpan diff=date2-date1;
③ String s1,s2;…if(s1==s2);
2 运算符有时比方法名更直观
如 两个复数用 a+b 比a.Add(b)更直观;
但要慎用;
3 运算符重载有一些限制
① 如成对,如类型要求,如有的不能重载;
二 运算符的声明
1 一元运算符声明的形式如下:
public static 类型 operator 一元运算符(类型 参数名){…}
2 二元运算符声明的形式如下:
public static 类型 operator 二元运算符(类型 参数名,类型 参数名){…};
3 类型转换运算符声明的形式如下:
public static implicit operator 类型(类型 参数名){…}
public static explicit operator 类型(类型 参数名)
{…}
复数的运算符重载
using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace 复数的运算符重载
{
public struct Complex
{
public double real;
public double imaginary;
public Complex(double real,double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator+(Complex c1)
{
return c1;
}
public static Complex operator-(Complex c1)
{
return new Complex(-c1.real, -c1.imaginary);
}
public static bool operator true(Complex c1)
{
return c1.real != 0 || c1.imaginary != 0;
}
public static bool operator false(Complex c1)
{
return c1.real == 0 && c1.imaginary == 0;
}
public static Complex operator+(Complex c1,Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static Complex operator-(Complex c1,Complex c2)
{
return c1 + (-c2);
}
public static Complex operator*(Complex c1,Complex c2)
{
return new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
c1.real * c2.imaginary + c1.imaginary * c2.real);
}
public static Complex operator*(Complex c,double k)
{
return new Complex(c.real * k, c.imaginary * k);
}
public static Complex operator*(double k,Complex c)
{
return c * k;
}
public override string ToString()
{
return (System.String.Format("{0}+{1}i)", real, imaginary));
}
public static void Main()
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);
Complex result = num1 ? -num1 * 5 + num1 * num2 : new Complex(0, 0);
System.Console.WriteLine("First complex number:{0}", num1);
System.Console.WriteLine("Second Complex number:{0}", num2);
System.Console.WriteLine("The result is:{0}", result);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
Digit类型定义了下面的操作符:
· 从Digit到byte的隐式转换操作符。
· 从byte到Digit的隐式转换操作符
· 把两个Digit数值相加并返回一个Digit数值的加法操作符。
· 把一共Digit数值与其它Digit数值相减,并返回一共digit数值的减法操作符。
· 比较两个digit数值的等式和非等式。
*/
namespace 全面的重载
{
public struct Digit
{
byte value;
public Digit(byte value)
{
if (value < 0 || value > 0) throw new ArgumentException();
this.value = value;
}
public Digit(int value) : this((byte)value) { }
public static implicit operator byte(Digit d)
{
return d.value;
}
public static explicit operator Digit(byte b)
{
return new Digit(b);
}
public static Digit operator+(Digit a,Digit b)
{
return new Digit(a.value + b.value);
}
public static Digit operator-(Digit a,Digit b)
{
return new Digit(a.value - b.value);
}
public static bool operator==(Digit a,Digit b)
{
return a.value == b.value;
}
public static bool operator!=(Digit a,Digit b)
{
return a.value != b.value;
}
public override bool Equals(object obj)
{
return this == (Digit)value;
}
public override int GetHashCode()
{
return value.GetHashCode();
}
public override string ToString()
{
return value.ToString();
}
}
class Test
{
static void Main()
{
Digit a = (Digit)5;
Digit b = (Digit)3;
Digit plus = a + b;
Digit minus = a - b;
bool equals = (a == b);
Console.WriteLine("{0}+{1}={2}", a, b, plus);
Console.WriteLine("{0}-{1}={2}", a, b, minus);
Console.WriteLine("{0}=={1}=={2}", a, b, equals);
Console.ReadKey();
}
}
}
相关文章
- C# 实现WebSocket服务端实例
- C#里面的枚举与位或运算符 一个枚举变量存入多个值
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
- 请求大神,C#如何截取字符串中指定字符之间的部分 按指定字符串分割 一分为二 c# 去除字符串中的某个已知字符
- c#关于路径的总结(转) 虚拟路径波浪号~和斜杠/的区别
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- Word控件Spire.Doc 【页面背景】教程(5) ;如如何在 C# 中为 Word 设置渐变背景
- C# (江湖小新)- 运算符重载(符号原来也可以当成方法用)
- C#学习记录——流程控制:布尔逻辑、布尔按位运算符和赋值运算符、运算符优先级的更新、分支
- c# 小技巧-单问号和双问号+位运算符
- c#重载运算符
- C# 中的 is 真的是越来越强大,越来越语义化
- c#中设计器中窗体释放 protected override void Dispose(bool disposing)改写解决部分窗体线程释放不彻底问题
- c#调用c++(Opencv)dll的实例
- C#位运算符的基本用法
- C#中包含?的运算符使用汇总
- 使用SHA256WithRSA来签名和验签(.NET/C#)
- c# as运算符
- 《C#零基础入门之百识百例》(十)其他运算符和优先级 -- 解方程式
- 《C#零基础入门之百识百例》(二十六)折半查找 -- 查找指定元素
- C# 中的MDI 窗体
- C# 中的数组类
- C# 向下转型 和 is运算符
- 【C#】基于JsonConvert解析Json数据
- C#-自定义类型转换与重写运算符,typeof()的使用
- C#.Net遍历远程文件夹下的所有文件 FTP
- C#-对MongoDB进行查询
- C#中的sleep()和wait()
- c#类的定义,c#中的关健字,C#标识符