//转换运算符具有以下特点:
//声明为 implicit 的转换在需要时自动进行。
//声明为 explicit 的转换需要调用强制转换。
//所有转换都必须是 static 转换。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class OperatorOverride
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public static void Execute()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Complex num1 = new Complex(12, 3);
Complex num2 = new Complex(16, 4);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//overload
Complex sum = num1 + num2;
Complex sum1 = num1 - num2;
Complex sum2 = num1 * num2;
Complex sum3 = num1 / num2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//implicit
double dd = sum;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//explicit
byte b = 3;
Digit d = (Digit)b;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//implicit
Digit digit = new Digit(3);
byte bb = digit;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Display
System.Console.WriteLine("complex number: {0}", sum);
System.Console.WriteLine("complex number: {0}", sum1);
System.Console.WriteLine("complex number: {0}", sum2);
System.Console.WriteLine("complex number: {0}", sum3);
System.Console.WriteLine("complex number: {0}", dd);
System.Console.WriteLine("complex number: {0}", d.GetType());
System.Console.WriteLine("complex number: {0}", bb);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public struct Complex
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int real;
int imaginary;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Complex(int real, int imaginary)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.real = real;
this.imaginary = imaginary;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//重载 + - * /
public static Complex operator +(Complex c1, Complex c2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Complex operator -(Complex c1, Complex c2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Complex operator *(Complex c1, Complex c2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new Complex(c1.real * c2.real, c1.imaginary * c2.imaginary);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Complex operator /(Complex c1, Complex c2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new Complex(c1.real / c2.real, c1.imaginary / c2.imaginary);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//隐式转换运算符
public static implicit operator double(Complex cc)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (double)cc.real / cc.imaginary;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (String.Format("{0}+{1}i", real, imaginary));
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
struct Digit
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
byte value;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Digit(byte value) //constructor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value > 9)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new System.ArgumentException();
}
this.value = value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//显式转换运算符
//此运算符将类型 Byte 转换为称为 Digit 的值类型。
//*由于不是所有字节都可以转换为数字,因此转换是显式的,这意味着必须使用强制转换
public static explicit operator Digit(byte b)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Digit d = new Digit(b);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Console.WriteLine("conversion occurred");
return d;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//隐式转换运算符
//由于任何数字都可以转换为 Byte,因此没有必要一定让用户知道进行的转换
public static implicit operator byte(Digit d)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return d.value;
}
}
有关属性:
如何访问基类中被派生类中具有同一名称的另一个属性隐藏的属性。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Employee
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Name 《《《----
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ name = value; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Manager : Employee
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public new string Name 《《《----
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ name = value; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
sealed class UserProperty
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public void execute()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Manager m = new Manager();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m.Name = "RuiLei";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
((Employee)m).Name = "Lei"; //Base Name
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Console.WriteLine("Name in the derived class is: {0}", m.Name);
System.Console.WriteLine("Name in the base class is: {0}", ((Employee)m).Name);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
比较有意思
复制构造函数:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Instance person = new Instance("George", 40);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//浅度赋值
Instance personFleet = person;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//深度赋值
//类是没有Clone方法的,需要继承ICloneable
//Instance personDeep = (Instance)person.Clone();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Console.WriteLine(personFleet.Details);
//System.Console.WriteLine(personDeep.Details);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
person.name = "AAA";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Console.WriteLine(personFleet.Details);
//System.Console.WriteLine(personDeep.Details);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////Fleet result:
/// George is 40
/// AAA is 40
///
///Deep result:
/// George is 40
/// George is 40
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[Serializable]
public class Instance : ICloneable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public string name;
private int age;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Copy constructor.
public Instance(Instance previousPerson)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
name = previousPerson.name;
age = previousPerson.age;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Instance constructor.
public Instance(string name, int age)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name = name;
this.age = age;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Get accessor.
public string Details
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return name + " is " + age.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ICloneable Members#region ICloneable Members
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public object Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return base.MemberwiseClone();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
posted @
2007-03-19 16:18
RicoRui
阅读(
709)
评论()
编辑
收藏
举报