操作符重载
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");
}
}
}
}
首先我们见过的在已有的类里面有操作符重载的类有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");
}
}
}
}
相关文章推荐
- 高德、百度、Google地图定位偏移以及坐标系转换
- codeforces (G) The Debut Album dp+滚动数组
- mce-inject使用方法
- vs2010出现转换到COFF期间失败:文件无效或损坏
- ucore_lab7
- iOS button加边框设置圆角
- iOS图片实现可拉伸不变形的处理操作
- Python中matplotlib pyplot常用的一些使用方法[Python 3.x]
- 第七届科技节获奖及建模论文相似度名单公示
- Unity3D
- 《 常见算法与数据结构》平衡查找树(2)——红黑树(附动画)
- yum 安装ffmpeg
- ios按钮点击导致程序崩溃
- hadoop学习之HDFS(2.1):linux下eclipse中配置hadoop-mapreduce开发环境并运行WordCount.java程序
- MSP430+DMA
- Myeclipse错误:Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project
- html5带下拉框的input
- 编程语言的起源故事
- Const,Const函数,Const变量,函数后面的Const
- 求用delphi编写的LRC校验位算法函数,急!!!