C#系列之运算符重载

目录

1.基本概念

2.基本语法

3.实例

4.使用

5.可重载与不可重载运算符。

①算数运算符都可以重载

②逻辑运算符

③位运算符

④条件运算符

⑤不可以重载的运算符


1.基本概念

让自定义类和结构体能够使用运算符。

关键字:operator.

特点:一定是一个公共的静态方法,返回值写在operator的前面,逻辑处理自定义。

作用:让自定义类和结构体对象可以进行运算。

注意:条件运算符需要成队实现,一个符号可以多个重载,不能使用ref和out. 

2.基本语法

public static 返回类型 operator 运算符(参数列表)

3.实例

class Point

{

public int x;

public int y;

public static Point operator+(Point p1,Point p2)

    {

   Point p=new Point();

   p.x=p1.x+p2.x;

   p.y=p1.y+p2.y;

   return p;

   }

}

public static Point operator+(Point p1,int value)
{
  Point p=new Point();
  p.x=p1.x+value;
  p.y=p1.y+value;
  return p;
}

4.使用

Point p=new Point();
p.x=1;
p.y=1;
Point p2=new Point();
p2.x=2;
p2.y=2;
Point p3=p+p2;
Point p4=p3+2;

可以重载。加法参数最多写两个。

5.可重载与不可重载运算符。

①算数运算符都可以重载

注意符号是一个参数还是两个参数。

public static Point operator-(Point p1,Point p2)
{
  return null;
}
public static Point operator(Point p1,Point p2)
{
return null;
}
public static Point operator/(Point p1,Point p2)
{
return null;
}
public static Point operator%(Point p1,Point p2)
{
return null;
}
public static Point operator++(Point p1)
{
return null;
}
public static Point operator--(Point p1)
{
return null;
}
②逻辑运算符

注意:符号需要两个参数还是一个参数,逻辑或和逻辑与是没有运算符重载的。

public static bool operator||(Point p1)
{
return false;
}
③位运算符

注意:符号需要一个还是两个参数。

public static Point operator|(Point p1,Point p2)
{
return null;
}
public static Point operator&(Point p1,Point p2)
{
return null;
}
public static Point operator^(Point p1,Point p2)
{
return null;
}
public static Point operator~(Point p1)
{
return null;
}
public static Point operator>>(Point p1,int num)
{
return null;
}
④条件运算符

条件运算符必须成对出现。

返回值一般是bool类型,也可以是其他的,相关符号必须配对实现。

public static bool operator>(Point p1,Point p2)
{
return false;
}
public static bool operator<(Point p1,Point p2)
{
return false;
}
public static bool operator>=(Point p1,Point p2)
{
return false;
}
public static bool operator<=(Point p1,Point p2)
{
return false;
}
public static bool operator==(Point p1,Point p2)
{
return false;
}
public static bool operator!=(Point p1,Point p2)
{
return false;
}
⑤不可以重载的运算符

逻辑与(&&),逻辑或(| |)

索引符[ ]

强转运算符()

特殊运算符

点(.)   三目运算符?:  赋值符号=

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中,可以通过重载运算符来实现矩阵的乘法运算符重载。通过重载乘法运算符,我们可以实现两个矩阵对象之间的乘法操作。重载乘法运算符的方法是在类中定义一个名为"operator*"的方法。该方法接受两个矩阵对象作为参数,并返回一个新的矩阵对象,表示两个矩阵的乘法结果。 下面是一个示例代码,展示了如何在C#中重载矩阵乘法运算符: ```csharp public class Matrix { // 矩阵的行数和列数 private int rows; private int columns; // 矩阵的数据 private int[,] data; // 构造函数 public Matrix(int rows, int columns) { this.rows = rows; this.columns = columns; this.data = new int * matrix2.data[k, j]; } result.data[i, j = sum; } } return result; } } // 示例用法 Matrix matrix1 = new Matrix(2, 3); Matrix matrix2 = new Matrix(3, 2); Matrix result = matrix1 * matrix2; ``` 在上面的示例中,我们定义了一个名为Matrix的类,其中包含了重载乘法运算符的方法。在使用时,我们可以创建两个矩阵对象matrix1和matrix2,并通过乘法运算符*将它们相乘得到结果矩阵result。 请注意,为了进行矩阵乘法运算,我们需要确保第一个矩阵的列数与第二个矩阵的行数相等。如果不满足这个条件,就无法进行矩阵乘法运算。 以上是关于C#矩阵乘法运算符重载的解释和示例代码。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C#运算符重载](https://blog.csdn.net/q269399361/article/details/52924869)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C#运算符重载](https://blog.csdn.net/cloproad/article/details/79028076)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nicole Potter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值