您的位置:首页 > 其它

操作符重载

2016-05-25 10:53 330 查看
操作符重载:对已有的操作符进行新的改造以便于更加广泛的运用。

首先我们见过的在已有的类里面有操作符重载的类有string,因为我们知道对于string类型,是可以进行“+”,运算的,例如:

string a,b,c

a = "123"; b = "456";

c = a + b;

Console.WriteLine(c);//输出结果为123456

那么这里的“+”,显然不是通常意义上的对与整型或者浮点型的加法。那么在string类中,就具有运算符重载函数。

c#里面允许被重载的运算符有:

1.一元操作符:++ ,--,!,~,(T),true, false;

2.二元操作符:+,-,*,/,%,&,|,^,<<,>>,==,!=,>,<,>=,<=;

这里面的(T)表示类型转换操作,他可以使得用户自定义的类型自动转化为其它类型,要通过explicit或者implicit关键字来说明要进行的是显式类型还是隐式类型转换。所谓显式类型表示要通过(目的类型k)x的形式来强行转换x的类型为:目的类型k,那么隐式类型表示的是无需强制转换直接就变了。

explicit:

class Celsius
{
public Celsius(float temp)
{
degrees = temp;
}
public static explicit operator Fahrenheit(Celsius c)
{
return new Fahrenheit((9.0f / 5.0f) * c.degrees + 32);
}
public float Degrees
{
get { return degrees; }
}
private float degrees;
}

class Fahrenheit
{
public Fahrenheit(float temp)
{
degrees = temp;
}
// Must be defined inside a class called Fahrenheit:
public static explicit operator Celsius(Fahrenheit fahr)
{
return new Celsius((5.0f / 9.0f) * (fahr.degrees - 32));
}
public float Degrees
{
get { return degrees; }
}
private float degrees;
}

class MainClass
{
static void Main()
{
Fahrenheit fahr = new Fahrenheit(100.0f);
Console.Write("{0} Fahrenheit", fahr.Degrees);
Celsius c = (Celsius)fahr;

Console.Write(" = {0} Celsius", c.Degrees);
Fahrenheit fahr2 = (Fahrenheit)c;
Console.WriteLine(" = {0} Fahrenheit", fahr2.Degrees);
}
}
// Output:
// 100 Fahrenheit = 37.77778 Celsius = 100 Fahrenheit

implicit:
class Digit
{
public Digit(double d) { val = d; }
public double val;
// ...other members

// User-defined conversion from Digit to double
public static implicit operator double(Digit d)
{
return d.val;
}
// User-defined conversion from double to Digit
public static implicit operator Digit(double d)
{
return new Digit(d);
}
}

class Program
{
static void Main(string[] args)
{
Digit dig = new Digit(7);
//This call invokes the implicit "double" operator
double num = dig;
//This call invokes the implicit "Digit" operator
Digit dig2 = 12;
Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
Console.ReadLine();
}
}

加深一下理解。
下面这个是一些简单的操作符重载函数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
public class Prime
{
private uint x;
public Prime(uint x)
{
this.x = x;
}
public static uint operator + (Prime p1, Prime p2 )//对于加法的操作符重载
{
return p1.x + p2.x;
}
public static uint operator - (Prime p1, Prime p2 )
{
return p1.x - p2.x;
}
public static explicit operator uint(Prime p)//对于强制转换的重载
{
return p.x;
}
public static bool operator true(Prime p)//bool类型的重载,这里的true,返回的结果是否是真
{
for(uint i = 2;i*i <= p.x;i++ )
{
if (p.x % i == 0) return false;//返回的结果不是真
}
return true;//返回的结果是真
}
public static bool operator false(Prime p)
{
for(uint i = 2;i*i <= p.x;i++ )
{
if(p.x % i == 0) return true;//返回的结果是假
}
return false;//返回的结果不是假
}
}
static void Main(string[] args)
{
while(true)
{
string r = Console.ReadLine();
uint k = uint.Parse(r);
Prime p = new Prime(k);
if (p) Console.WriteLine("YES");
else Console.WriteLine("NO");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: