运算符重载 operator+[纯属温习啊][附加了一些内容 如:同名属性,复制构造函数]...

本文探讨了C#中的运算符重载技术,包括算术运算符的重载实现以及转换运算符的使用,并展示了如何处理基类属性在派生类中被隐藏的情况。同时介绍了复制构造函数的概念及其在实例间的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

None.gif//转换运算符具有以下特点:
None.gif    
//声明为 implicit 的转换在需要时自动进行。
None.gif    
//声明为 explicit 的转换需要调用强制转换。
None.gif    
//所有转换都必须是 static 转换。
None.gif

None.gif    
public class OperatorOverride
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public static void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Complex num1 
= new Complex(123);
InBlock.gif            Complex num2 
= new Complex(164);
InBlock.gif
InBlock.gif            
//overload
InBlock.gif
            Complex sum = num1 + num2;
InBlock.gif            Complex sum1 
= num1 - num2;
InBlock.gif            Complex sum2 
= num1 * num2;
InBlock.gif            Complex sum3 
= num1 / num2;
InBlock.gif
InBlock.gif            
//implicit
InBlock.gif
            double dd = sum;
InBlock.gif
InBlock.gif            
//explicit
InBlock.gif
            byte b = 3;
InBlock.gif            Digit d 
= (Digit)b;
InBlock.gif
InBlock.gif            
//implicit
InBlock.gif
            Digit digit = new Digit(3);
InBlock.gif            
byte bb = digit;
InBlock.gif
InBlock.gif            
//Display
InBlock.gif
            System.Console.WriteLine("complex number:  {0}", sum);
InBlock.gif            System.Console.WriteLine(
"complex number:  {0}", sum1);
InBlock.gif            System.Console.WriteLine(
"complex number:  {0}", sum2);
InBlock.gif            System.Console.WriteLine(
"complex number:  {0}", sum3);
InBlock.gif            System.Console.WriteLine(
"complex number:  {0}", dd);
InBlock.gif            System.Console.WriteLine(
"complex number:  {0}", d.GetType());
InBlock.gif            System.Console.WriteLine(
"complex number:  {0}", bb);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public struct Complex
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
int real;
InBlock.gif        
int imaginary;
InBlock.gif
InBlock.gif        
public Complex(int real, int imaginary)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.real = real;
InBlock.gif            
this.imaginary = imaginary;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//重载 + - * /
InBlock.gif
        public static Complex operator +(Complex c1, Complex c2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static Complex operator -(Complex c1, Complex c2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static Complex operator *(Complex c1, Complex c2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Complex(c1.real * c2.real, c1.imaginary * c2.imaginary);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static Complex operator /(Complex c1, Complex c2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Complex(c1.real / c2.real, c1.imaginary / c2.imaginary);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//隐式转换运算符
InBlock.gif
        public static implicit operator double(Complex cc)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (double)cc.real / cc.imaginary;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (String.Format("{0}+{1}i", real, imaginary));
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
struct Digit
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
byte value;
InBlock.gif
InBlock.gif        
public Digit(byte value)  //constructor
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if (value > 9)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new System.ArgumentException();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.value = value;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//显式转换运算符
InBlock.gif        
//此运算符将类型 Byte 转换为称为 Digit 的值类型。
InBlock.gif        
//*由于不是所有字节都可以转换为数字,因此转换是显式的,这意味着必须使用强制转换
InBlock.gif
        public static explicit operator Digit(byte b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Digit d 
= new Digit(b);
InBlock.gif
InBlock.gif            System.Console.WriteLine(
"conversion occurred");
InBlock.gif            
return d;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//隐式转换运算符
InBlock.gif        
//由于任何数字都可以转换为 Byte,因此没有必要一定让用户知道进行的转换
InBlock.gif
        public static implicit operator byte(Digit d)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return d.value;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif有关属性:
None.gif如何访问基类中被派生类中具有同一名称的另一个属性隐藏的属性。
None.gif
None.gif
public class Employee
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string name;
InBlock.gif
InBlock.gif        
public string Name   《《《----
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public class Manager : Employee
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string name;
InBlock.gif
InBlock.gif        
public new string Name   《《《----
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif    
ExpandedBlockEnd.gif    }

None.gif
None.gif    
sealed class UserProperty
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public void execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Manager m 
= new Manager();
InBlock.gif
InBlock.gif            m.Name 
= "RuiLei";   
InBlock.gif
InBlock.gif            ((Employee)m).Name 
= "Lei";  //Base Name
InBlock.gif

InBlock.gif            System.Console.WriteLine(
"Name in the derived class is: {0}", m.Name);
InBlock.gif            System.Console.WriteLine(
"Name in the base class is: {0}", ((Employee)m).Name);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

比较有意思
None.gif复制构造函数:
None.gif
None.gif
static void Main(string[] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Instance person 
= new Instance("George"40);
InBlock.gif
InBlock.gif            
//浅度赋值
InBlock.gif
            Instance personFleet = person;
InBlock.gif
InBlock.gif            
//深度赋值
InBlock.gif            
//类是没有Clone方法的,需要继承ICloneable
InBlock.gif            
//Instance personDeep = (Instance)person.Clone();
InBlock.gif

InBlock.gif            System.Console.WriteLine(personFleet.Details);
InBlock.gif            
//System.Console.WriteLine(personDeep.Details);
InBlock.gif

InBlock.gif            person.name 
= "AAA";
InBlock.gif
InBlock.gif            System.Console.WriteLine(personFleet.Details);
InBlock.gif            
//System.Console.WriteLine(personDeep.Details);
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////Fleet result:
InBlock.gif            
///     George is 40
InBlock.gif            
///     AAA is 40
InBlock.gif            
/// 
InBlock.gif            
///Deep result:
InBlock.gif            
///     George is 40
ExpandedSubBlockEnd.gif            
///     George is 40

InBlock.gif            
ExpandedBlockEnd.gif        }

None.gif
None.gif[Serializable]
None.gif    
public class Instance : ICloneable
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public string name;
InBlock.gif        
private int age;
InBlock.gif
InBlock.gif        
// Copy constructor.
InBlock.gif
        public Instance(Instance previousPerson)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            name 
= previousPerson.name;
InBlock.gif            age 
= previousPerson.age;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Instance constructor.
InBlock.gif
        public Instance(string name, int age)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.name = name;
InBlock.gif            
this.age = age;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Get accessor.
InBlock.gif
        public string Details
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return name + " is " + age.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ICloneable Members#region ICloneable Members
InBlock.gif
InBlock.gif        
public object Clone()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return base.MemberwiseClone();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/RuiLei/archive/2007/03/19/679983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值