运算符重载,以及迭代器[foreach]示例
生活随笔
收集整理的這篇文章主要介紹了
运算符重载,以及迭代器[foreach]示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以下代碼來源于"c#高級編程(第4版)",只不過我對它做了一些注釋和修改
using?System;
using?System.Collections;
using?System.Text;
namespace?Wrox.ProCSharp.VectorAsCollection
{
????class?MainEntryPoint
????{
????????static?void?Main(string[]?args)
????????{
????????????Vector?Vect1?=?new?Vector(1.0,?2.0,?5.0);
????????????Console.WriteLine(Vect1.ToString());
????????????Console.WriteLine(Vect1.ToString("IJK",null));
????????????Console.WriteLine(Vect1.ToString("VE",?null));
????????????Vector?Vect2?=?new?Vector(1.0,?2.0,?3.0);
????????????Console.WriteLine(Vect1?==?Vect2);
????????????Double?t?=?Vect1?*?Vect2;
????????????Console.WriteLine(t);
????????????foreach?(double?p?in?Vect1)
????????????{
????????????????Console.WriteLine(p);
????????????}
????????????Console.ReadLine();
????????}
????}
????/**////?<summary>
????///?聲明一個矢量類(包含基本的x,y,z三個分量)
????///?</summary>
????struct?Vector?:?IFormattable
????{
????????public?double?x,?y,?z;???????
????????public?Vector(double?x,?double?y,?double?z)
????????{
????????????this.x?=?x;
????????????this.y?=?y;
????????????this.z?=?z;
????????}
????????
????????/**////?<summary>
????????///?構造函數
????????///?</summary>
????????///?<param?name="rhs"></param>
????????public?Vector(Vector?rhs)
????????{
????????????x?=?rhs.x;
????????????y?=?rhs.y;
????????????z?=?rhs.z;
????????}
????????/**////?<summary>
????????///?重載object類的ToString()方法
????????///?</summary>
????????///?<returns></returns>
????????public?override?string?ToString()
????????{
????????????return?"(?"?+?x?+?"?,?"?+?y?+?"?,?"?+?z?+?"?)";
????????}
????????/**////?<summary>
????????///?實現IFromattable接口,擴展自己的ToString()方法
????????///?</summary>
????????///?<param?name="format"></param>
????????///?<param?name="formatProvider"></param>
????????///?<returns></returns>
????????public?string?ToString(string?format,?IFormatProvider?formatProvider)
????????{
????????????if?(format?==?null)
????????????????return?ToString();
????????????string?formatUpper?=?format.ToUpper();
????????????switch?(formatUpper)
????????????{
????????????????case?"N":
????????????????????return?"||?"?+?Norm().ToString()?+?"?||";
????????????????case?"VE":
????????????????????return?String.Format("(?{0:E},?{1:E},?{2:E}?)",?x,?y,?z);
????????????????case?"IJK":
????????????????????StringBuilder?sb?=?new?StringBuilder(x.ToString(),?30);
????????????????????sb.Append("?i?+?");
????????????????????sb.Append(y.ToString());
????????????????????sb.Append("?j?+?");
????????????????????sb.Append(z.ToString());
????????????????????sb.Append("?k");
????????????????????return?sb.ToString();
????????????????default:
????????????????????return?ToString();
????????????}
????????}
????????/**////?<summary>
????????///?索引器
????????///?</summary>
????????///?<param?name="i"></param>
????????///?<returns></returns>
????????public?double?this[uint?i]
????????{
????????????get
????????????{
????????????????switch?(i)
????????????????{
????????????????????case?0:
????????????????????????return?x;
????????????????????case?1:
????????????????????????return?y;
????????????????????case?2:
????????????????????????return?z;
????????????????????default:
????????????????????????throw?new?IndexOutOfRangeException(
???????????????????????????"Attempt?to?retrieve?Vector?element"?+?i);
????????????????}
????????????}
????????????set
????????????{
????????????????switch?(i)
????????????????{
????????????????????case?0:
????????????????????????x?=?value;
????????????????????????break;
????????????????????case?1:
????????????????????????y?=?value;
????????????????????????break;
????????????????????case?2:
????????????????????????z?=?value;
????????????????????????break;
????????????????????default:
????????????????????????throw?new?IndexOutOfRangeException(
???????????????????????????"Attempt?to?set?Vector?element"?+?i);
????????????????}
????????????}
????????}
????????/**//*??
?????????*?考慮到精度問題,暫不用下面的寫法來實現==運算符的重載
?????????*?public?static?bool?operator?==?(Vector?lhs,?Vector?rhs)
?????????????????{
????????????????????if?(lhs.x?==?rhs.x?&&?lhs.y?==?rhs.y?&&?lhs.z?==?rhs.z)
???????????????????????return?true;
????????????????????else
???????????????????????return?false;
?????????????????}*/
????????private?const?double?Epsilon?=?0.0000001;
????????
????????/**////?<summary>
????????///?重載==運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?bool?operator?==(Vector?lhs,?Vector?rhs)
????????{
????????????if?(System.Math.Abs(lhs.x?-?rhs.x)?<?Epsilon?&&
???????????????System.Math.Abs(lhs.y?-?rhs.y)?<?Epsilon?&&
???????????????System.Math.Abs(lhs.z?-?rhs.z)?<?Epsilon)
????????????????return?true;
????????????else
????????????????return?false;
????????}
????????/**////?<summary>
????????///?重載!=運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?bool?operator?!=(Vector?lhs,?Vector?rhs)
????????{
????????????return?!(lhs?==?rhs);
????????}
????????/**////?<summary>
????????///?重載+運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?Vector?operator?+(Vector?lhs,?Vector?rhs)
????????{
????????????Vector?Result?=?new?Vector(lhs);
????????????Result.x?+=?rhs.x;
????????????Result.y?+=?rhs.y;
????????????Result.z?+=?rhs.z;
????????????return?Result;
????????}
????????/**////?<summary>
????????///?重載*運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?Vector?operator?*(double?lhs,?Vector?rhs)
????????{
????????????return?new?Vector(lhs?*?rhs.x,?lhs?*?rhs.y,?lhs?*?rhs.z);
????????}
????????/**////?<summary>
????????///?重載*運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?Vector?operator?*(Vector?lhs,?double?rhs)
????????{
????????????return?rhs?*?lhs;
????????}
????????/**////?<summary>
????????///?重載*運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?double?operator?*(Vector?lhs,?Vector?rhs)
????????{
????????????return?lhs.x?*?rhs.x?+?lhs.y?+?rhs.y?+?lhs.z?*?rhs.z;
????????}
????????/**////?<summary>
????????///?重載GetHashCode(不重載的話,也可編譯通過,但編譯時會有一個警告)
????????///?</summary>
????????///?<returns></returns>
????????public?override?int?GetHashCode()
????????{
????????????????return?base.GetHashCode();
????????}
????????/**////?<summary>
????????///?重載Equals(不重載的話,也可編譯通過,但編譯時會有一個警告)
????????///?</summary>
????????///?<param?name="obj"></param>
????????///?<returns></returns>
????????public?override?bool?Equals(object?obj)
????????{
????????????????return?base.Equals(obj);
????????}
????????/**////?<summary>
????????///?返回x,y,z的平方和
????????///?</summary>
????????///?<returns></returns>
????????public?double?Norm()
????????{
????????????return?x?*?x?+?y?*?y?+?z?*?z;
????????}
????????enumerator?class#region?enumerator?class
????????
????????public?IEnumerator?GetEnumerator()
????????{
????????????return?new?VectorEnumerator(this);
????????}
???????
????????private?class?VectorEnumerator?:?IEnumerator?//要使用佚代器,必須實現IEnumerator接口
????????{
????????????Vector?theVector;??????//?Vector?object?that?this?enumerato?refers?to?
????????????int?location;???//?which?element?of?theVector?the?enumerator?is?currently?referring?to?
????????????public?VectorEnumerator(Vector?theVector)
????????????{
????????????????this.theVector?=?theVector;
????????????????location?=?-1;
????????????}
????????????public?bool?MoveNext()
????????????{
????????????????++location;
????????????????return?(location?>?2)???false?:?true;
????????????}
????????????public?object?Current
????????????{
????????????????get
????????????????{
????????????????????if?(location?<?0?||?location?>?2)
????????????????????????throw?new?InvalidOperationException(
???????????????????????????"The?enumerator?is?either?before?the?first?element?or?"?+
???????????????????????????"after?the?last?element?of?the?Vector");
????????????????????return?theVector[(uint)location];
????????????????}
????????????}
????????????public?void?Reset()
????????????{
????????????????location?=?-1;
????????????}
????????}
????????#endregion
????}
} 運行結果:
( 1 , 2 , 5 )
1 i + 2 j + 5 k
( 1.000000E+000, 2.000000E+000, 5.000000E+000 )
False
20
1
2
5
using?System;
using?System.Collections;
using?System.Text;
namespace?Wrox.ProCSharp.VectorAsCollection
{
????class?MainEntryPoint
????{
????????static?void?Main(string[]?args)
????????{
????????????Vector?Vect1?=?new?Vector(1.0,?2.0,?5.0);
????????????Console.WriteLine(Vect1.ToString());
????????????Console.WriteLine(Vect1.ToString("IJK",null));
????????????Console.WriteLine(Vect1.ToString("VE",?null));
????????????Vector?Vect2?=?new?Vector(1.0,?2.0,?3.0);
????????????Console.WriteLine(Vect1?==?Vect2);
????????????Double?t?=?Vect1?*?Vect2;
????????????Console.WriteLine(t);
????????????foreach?(double?p?in?Vect1)
????????????{
????????????????Console.WriteLine(p);
????????????}
????????????Console.ReadLine();
????????}
????}
????/**////?<summary>
????///?聲明一個矢量類(包含基本的x,y,z三個分量)
????///?</summary>
????struct?Vector?:?IFormattable
????{
????????public?double?x,?y,?z;???????
????????public?Vector(double?x,?double?y,?double?z)
????????{
????????????this.x?=?x;
????????????this.y?=?y;
????????????this.z?=?z;
????????}
????????
????????/**////?<summary>
????????///?構造函數
????????///?</summary>
????????///?<param?name="rhs"></param>
????????public?Vector(Vector?rhs)
????????{
????????????x?=?rhs.x;
????????????y?=?rhs.y;
????????????z?=?rhs.z;
????????}
????????/**////?<summary>
????????///?重載object類的ToString()方法
????????///?</summary>
????????///?<returns></returns>
????????public?override?string?ToString()
????????{
????????????return?"(?"?+?x?+?"?,?"?+?y?+?"?,?"?+?z?+?"?)";
????????}
????????/**////?<summary>
????????///?實現IFromattable接口,擴展自己的ToString()方法
????????///?</summary>
????????///?<param?name="format"></param>
????????///?<param?name="formatProvider"></param>
????????///?<returns></returns>
????????public?string?ToString(string?format,?IFormatProvider?formatProvider)
????????{
????????????if?(format?==?null)
????????????????return?ToString();
????????????string?formatUpper?=?format.ToUpper();
????????????switch?(formatUpper)
????????????{
????????????????case?"N":
????????????????????return?"||?"?+?Norm().ToString()?+?"?||";
????????????????case?"VE":
????????????????????return?String.Format("(?{0:E},?{1:E},?{2:E}?)",?x,?y,?z);
????????????????case?"IJK":
????????????????????StringBuilder?sb?=?new?StringBuilder(x.ToString(),?30);
????????????????????sb.Append("?i?+?");
????????????????????sb.Append(y.ToString());
????????????????????sb.Append("?j?+?");
????????????????????sb.Append(z.ToString());
????????????????????sb.Append("?k");
????????????????????return?sb.ToString();
????????????????default:
????????????????????return?ToString();
????????????}
????????}
????????/**////?<summary>
????????///?索引器
????????///?</summary>
????????///?<param?name="i"></param>
????????///?<returns></returns>
????????public?double?this[uint?i]
????????{
????????????get
????????????{
????????????????switch?(i)
????????????????{
????????????????????case?0:
????????????????????????return?x;
????????????????????case?1:
????????????????????????return?y;
????????????????????case?2:
????????????????????????return?z;
????????????????????default:
????????????????????????throw?new?IndexOutOfRangeException(
???????????????????????????"Attempt?to?retrieve?Vector?element"?+?i);
????????????????}
????????????}
????????????set
????????????{
????????????????switch?(i)
????????????????{
????????????????????case?0:
????????????????????????x?=?value;
????????????????????????break;
????????????????????case?1:
????????????????????????y?=?value;
????????????????????????break;
????????????????????case?2:
????????????????????????z?=?value;
????????????????????????break;
????????????????????default:
????????????????????????throw?new?IndexOutOfRangeException(
???????????????????????????"Attempt?to?set?Vector?element"?+?i);
????????????????}
????????????}
????????}
????????/**//*??
?????????*?考慮到精度問題,暫不用下面的寫法來實現==運算符的重載
?????????*?public?static?bool?operator?==?(Vector?lhs,?Vector?rhs)
?????????????????{
????????????????????if?(lhs.x?==?rhs.x?&&?lhs.y?==?rhs.y?&&?lhs.z?==?rhs.z)
???????????????????????return?true;
????????????????????else
???????????????????????return?false;
?????????????????}*/
????????private?const?double?Epsilon?=?0.0000001;
????????
????????/**////?<summary>
????????///?重載==運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?bool?operator?==(Vector?lhs,?Vector?rhs)
????????{
????????????if?(System.Math.Abs(lhs.x?-?rhs.x)?<?Epsilon?&&
???????????????System.Math.Abs(lhs.y?-?rhs.y)?<?Epsilon?&&
???????????????System.Math.Abs(lhs.z?-?rhs.z)?<?Epsilon)
????????????????return?true;
????????????else
????????????????return?false;
????????}
????????/**////?<summary>
????????///?重載!=運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?bool?operator?!=(Vector?lhs,?Vector?rhs)
????????{
????????????return?!(lhs?==?rhs);
????????}
????????/**////?<summary>
????????///?重載+運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?Vector?operator?+(Vector?lhs,?Vector?rhs)
????????{
????????????Vector?Result?=?new?Vector(lhs);
????????????Result.x?+=?rhs.x;
????????????Result.y?+=?rhs.y;
????????????Result.z?+=?rhs.z;
????????????return?Result;
????????}
????????/**////?<summary>
????????///?重載*運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?Vector?operator?*(double?lhs,?Vector?rhs)
????????{
????????????return?new?Vector(lhs?*?rhs.x,?lhs?*?rhs.y,?lhs?*?rhs.z);
????????}
????????/**////?<summary>
????????///?重載*運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?Vector?operator?*(Vector?lhs,?double?rhs)
????????{
????????????return?rhs?*?lhs;
????????}
????????/**////?<summary>
????????///?重載*運算符
????????///?</summary>
????????///?<param?name="lhs"></param>
????????///?<param?name="rhs"></param>
????????///?<returns></returns>
????????public?static?double?operator?*(Vector?lhs,?Vector?rhs)
????????{
????????????return?lhs.x?*?rhs.x?+?lhs.y?+?rhs.y?+?lhs.z?*?rhs.z;
????????}
????????/**////?<summary>
????????///?重載GetHashCode(不重載的話,也可編譯通過,但編譯時會有一個警告)
????????///?</summary>
????????///?<returns></returns>
????????public?override?int?GetHashCode()
????????{
????????????????return?base.GetHashCode();
????????}
????????/**////?<summary>
????????///?重載Equals(不重載的話,也可編譯通過,但編譯時會有一個警告)
????????///?</summary>
????????///?<param?name="obj"></param>
????????///?<returns></returns>
????????public?override?bool?Equals(object?obj)
????????{
????????????????return?base.Equals(obj);
????????}
????????/**////?<summary>
????????///?返回x,y,z的平方和
????????///?</summary>
????????///?<returns></returns>
????????public?double?Norm()
????????{
????????????return?x?*?x?+?y?*?y?+?z?*?z;
????????}
????????enumerator?class#region?enumerator?class
????????
????????public?IEnumerator?GetEnumerator()
????????{
????????????return?new?VectorEnumerator(this);
????????}
???????
????????private?class?VectorEnumerator?:?IEnumerator?//要使用佚代器,必須實現IEnumerator接口
????????{
????????????Vector?theVector;??????//?Vector?object?that?this?enumerato?refers?to?
????????????int?location;???//?which?element?of?theVector?the?enumerator?is?currently?referring?to?
????????????public?VectorEnumerator(Vector?theVector)
????????????{
????????????????this.theVector?=?theVector;
????????????????location?=?-1;
????????????}
????????????public?bool?MoveNext()
????????????{
????????????????++location;
????????????????return?(location?>?2)???false?:?true;
????????????}
????????????public?object?Current
????????????{
????????????????get
????????????????{
????????????????????if?(location?<?0?||?location?>?2)
????????????????????????throw?new?InvalidOperationException(
???????????????????????????"The?enumerator?is?either?before?the?first?element?or?"?+
???????????????????????????"after?the?last?element?of?the?Vector");
????????????????????return?theVector[(uint)location];
????????????????}
????????????}
????????????public?void?Reset()
????????????{
????????????????location?=?-1;
????????????}
????????}
????????#endregion
????}
} 運行結果:
( 1 , 2 , 5 )
1 i + 2 j + 5 k
( 1.000000E+000, 2.000000E+000, 5.000000E+000 )
False
20
1
2
5
轉載于:https://www.cnblogs.com/yjmyzz/archive/2007/12/16/996979.html
總結
以上是生活随笔為你收集整理的运算符重载,以及迭代器[foreach]示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ProMesh.Net基本使用说明!
- 下一篇: [原创软件测试工作技能