运算符重载 operator+[纯属温习啊][附加了一些内容 如:同名属性,复制构造函数]
//转换运算符具有以下特点:
//声明为 implicit 的转换在需要时自动进行。
//声明为 explicit 的转换需要调用强制转换。
//所有转换都必须是 static 转换。
public class OperatorOverride
{
public static void Execute()
{
Complex num1 = new Complex(12, 3);
Complex num2 = new Complex(16, 4);
//overload
Complex sum = num1 + num2;
Complex sum1 = num1 - num2;
Complex sum2 = num1 * num2;
Complex sum3 = num1 / num2;
//implicit
double dd = sum;
//explicit
byte b = 3;
Digit d = (Digit)b;
//implicit
Digit digit = new Digit(3);
byte bb = digit;
//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);
}
}
public struct Complex
{
int real;
int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
//重载 + - * /
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real, c1.imaginary * c2.imaginary);
}
public static Complex operator /(Complex c1, Complex c2)
{
return new Complex(c1.real / c2.real, c1.imaginary / c2.imaginary);
}
//隐式转换运算符
public static implicit operator double(Complex cc)
{
return (double)cc.real / cc.imaginary;
}
public override string ToString()
{
return (String.Format("{0}+{1}i", real, imaginary));
}
}
struct Digit
{
byte value;
public Digit(byte value) //constructor
{
if (value > 9)
{
throw new System.ArgumentException();
}
this.value = value;
}
//显式转换运算符
//此运算符将类型 Byte 转换为称为 Digit 的值类型。
//*由于不是所有字节都可以转换为数字,因此转换是显式的,这意味着必须使用强制转换
public static explicit operator Digit(byte b)
{
Digit d = new Digit(b);
System.Console.WriteLine("conversion occurred");
return d;
}
//隐式转换运算符
//由于任何数字都可以转换为 Byte,因此没有必要一定让用户知道进行的转换
public static implicit operator byte(Digit d)
{
return d.value;
}
}
//声明为 implicit 的转换在需要时自动进行。
//声明为 explicit 的转换需要调用强制转换。
//所有转换都必须是 static 转换。
public class OperatorOverride
{
public static void Execute()
{
Complex num1 = new Complex(12, 3);
Complex num2 = new Complex(16, 4);
//overload
Complex sum = num1 + num2;
Complex sum1 = num1 - num2;
Complex sum2 = num1 * num2;
Complex sum3 = num1 / num2;
//implicit
double dd = sum;
//explicit
byte b = 3;
Digit d = (Digit)b;
//implicit
Digit digit = new Digit(3);
byte bb = digit;
//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);
}
}
public struct Complex
{
int real;
int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
//重载 + - * /
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real, c1.imaginary * c2.imaginary);
}
public static Complex operator /(Complex c1, Complex c2)
{
return new Complex(c1.real / c2.real, c1.imaginary / c2.imaginary);
}
//隐式转换运算符
public static implicit operator double(Complex cc)
{
return (double)cc.real / cc.imaginary;
}
public override string ToString()
{
return (String.Format("{0}+{1}i", real, imaginary));
}
}
struct Digit
{
byte value;
public Digit(byte value) //constructor
{
if (value > 9)
{
throw new System.ArgumentException();
}
this.value = value;
}
//显式转换运算符
//此运算符将类型 Byte 转换为称为 Digit 的值类型。
//*由于不是所有字节都可以转换为数字,因此转换是显式的,这意味着必须使用强制转换
public static explicit operator Digit(byte b)
{
Digit d = new Digit(b);
System.Console.WriteLine("conversion occurred");
return d;
}
//隐式转换运算符
//由于任何数字都可以转换为 Byte,因此没有必要一定让用户知道进行的转换
public static implicit operator byte(Digit d)
{
return d.value;
}
}
有关属性:
如何访问基类中被派生类中具有同一名称的另一个属性隐藏的属性。
public class Employee
{
private string name;
public string Name 《《《----
{
get { return name; }
set { name = value; }
}
}
public class Manager : Employee
{
private string name;
public new string Name 《《《----
{
get { return name; }
set { name = value; }
}
}
sealed class UserProperty
{
public void execute()
{
Manager m = new Manager();
m.Name = "RuiLei";
((Employee)m).Name = "Lei"; //Base Name
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);
}
}
如何访问基类中被派生类中具有同一名称的另一个属性隐藏的属性。
public class Employee
{
private string name;
public string Name 《《《----
{
get { return name; }
set { name = value; }
}
}
public class Manager : Employee
{
private string name;
public new string Name 《《《----
{
get { return name; }
set { name = value; }
}
}
sealed class UserProperty
{
public void execute()
{
Manager m = new Manager();
m.Name = "RuiLei";
((Employee)m).Name = "Lei"; //Base Name
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);
}
}
比较有意思
复制构造函数:
static void Main(string[] args)
{
Instance person = new Instance("George", 40);
//浅度赋值
Instance personFleet = person;
//深度赋值
//类是没有Clone方法的,需要继承ICloneable
//Instance personDeep = (Instance)person.Clone();
System.Console.WriteLine(personFleet.Details);
//System.Console.WriteLine(personDeep.Details);
person.name = "AAA";
System.Console.WriteLine(personFleet.Details);
//System.Console.WriteLine(personDeep.Details);
///Fleet result:
/// George is 40
/// AAA is 40
///
///Deep result:
/// George is 40
/// George is 40
}
[Serializable]
public class Instance : ICloneable
{
public string name;
private int age;
// Copy constructor.
public Instance(Instance previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}
// Instance constructor.
public Instance(string name, int age)
{
this.name = name;
this.age = age;
}
// Get accessor.
public string Details
{
get
{
return name + " is " + age.ToString();
}
}
ICloneable Members
}
static void Main(string[] args)
{
Instance person = new Instance("George", 40);
//浅度赋值
Instance personFleet = person;
//深度赋值
//类是没有Clone方法的,需要继承ICloneable
//Instance personDeep = (Instance)person.Clone();
System.Console.WriteLine(personFleet.Details);
//System.Console.WriteLine(personDeep.Details);
person.name = "AAA";
System.Console.WriteLine(personFleet.Details);
//System.Console.WriteLine(personDeep.Details);
///Fleet result:
/// George is 40
/// AAA is 40
///
///Deep result:
/// George is 40
/// George is 40
}
[Serializable]
public class Instance : ICloneable
{
public string name;
private int age;
// Copy constructor.
public Instance(Instance previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}
// Instance constructor.
public Instance(string name, int age)
{
this.name = name;
this.age = age;
}
// Get accessor.
public string Details
{
get
{
return name + " is " + age.ToString();
}
}
ICloneable Members
}