C#運算子多載

來源:互聯網
上載者:User

標籤:

一、C#運算子多載

1. C#運算子多載

運算子多載是指允許使用者使用自己定義的類型編寫運算子號,同時還允許使用者定義的類型與預定義的類型具有相同的功能。

重載運算子的目的是方便我們自己的類或結構使用。所有運算子多載均為類或結構的靜態方法。

因此,在C#語言中,允許使用者定義的類型通過使用operator關鍵字定義成靜態成員函數的形式來重載運算子。operator關鍵字用於在類或結構聲明中聲明運算子。

2. 運算子的可重載性

在C#中,並不是所有的運算子都可以被重載的。下表列出了運算子的可重載性:

注意:如果重載比較子,就必須成對重載;也就是說,如果重載“==”,也必須重載“!=”。反之也是這樣。“<”和“>”以及“<=”和“>=”同樣如此。

3. 重載一元運算子

一元運算子只有一個參數。

public static return-type operator op(type operand)
{
    // statement;
}

4. 重載二元運算子

二元運算子有兩個參數。

public static return-type operator op(type operand1, type operand2)
{
    // statement;
}

二、提示
 
C#運算子多載的聲明方式與方法的聲明方式相同,但operator關鍵字告訴編譯器,它實際上是一個運算子多載,後面是相關運算子的符號,傳回型別是在使用這個運算子時獲得的類型。

C#要求所有的C#運算子多載都聲明為public和static,這表示它們與它們的類或結構相關聯,而不是與執行個體相關聯所以運算子多載的代碼體不能訪問非靜態成員,也不能訪問this標識符。

三、樣本
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{

  public class ComplexNumber
  {
    private int real;
    private int imaginary;
    public ComplexNumber() : this(0, 0)  // constructor
    {
    }
    public ComplexNumber(int r, int i)  // constructor
    {
      real = r;
      imaginary = i;
    }
    //Override ToString() to display a complex number in the traditional format
    public override string ToString()
    {
      return(System.String.Format("{0} + {1}i", real, imaginary));
    }
    // 重載“+”操作符
    public static ComplexNumber operator+(ComplexNumber a, ComplexNumber b)
    {
      return new ComplexNumber(a.real + b.real, a.imaginary + b.imaginary);
    }
    // 重載“‘-”操作符
    public static ComplexNumber operator-(ComplexNumber a, ComplexNumber b)
    {
      return new ComplexNumber(a.real - b.real, a.imaginary - b.imaginary);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      // C#運算子多載-www.baike369.com
      ComplexNumber a = new ComplexNumber(10, 12);
      ComplexNumber b = new ComplexNumber(8, 9);
      Console.WriteLine("Complex Number a = {0}", a.ToString());
      Console.WriteLine("Complex Number b = {0}", b.ToString()); 
      ComplexNumber sum = a + b;
      Console.WriteLine("Complex Number sum = {0}", sum.ToString());
      ComplexNumber difference = a - b;
      Console.WriteLine("Complex Number difference = {0}", 
              difference.ToString());
      Console.ReadLine();
    }
  }
}

運行結果:
  
Complex Number a = 10 + 12i
Complex Number b = 8 + 9i
Complex Number sum = 18 + 21i
Complex Number difference = 2 + 3i

 

C#運算子多載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.