برنامه سازي پيشرفته 1 4. using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public.

Presentation on theme: "برنامه سازي پيشرفته 1 4. using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public."— Presentation transcript:

برنامه سازي پيشرفته 1 4

using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public void Print( ) {Console.WriteLine( "{0}/{1}", numerator, denominator );} } public class ReferenceTest { public static void Main( ) { Fraction f = new Fraction( ); f.numerator = 5; f.denominator = 10; f.Print( ); Fraction f2 = f;//f2 is a reference to f and not a copy!!! f.Print( ); //modify instance f2. Note that f is also effected. f2.numerator = 1; f.Print( ); f2.Print( ); } 2 2 f f2 وقتي از عملگر انتساب براي كلاس استفاده مي‌شود. f2 مرجعي براي f است و نه كپي f

تعريف مجدد عملگرها عملگرها بايد بصورت static تعريف شوند. عملگرها به يك نوع تعلق دارند، نه به يك نمونه. فرم تابع تعريف مجدد عملگر : public static return-type operator T (param p [,param p1]) 3

using System; public class Fraction { //data members private intm_numerator; private intm_denominator; //Properties public int Numerator { get { return m_numerator; } set { m_numerator = value; } } public int Denominator { get { return m_denominator; } set { m_denominator = value; } } //Constructors public Fraction( ) { m_numerator = 0; m_denominator = 0; } public Fraction( int iNumerator, int iDenominator ) { m_numerator = iNumerator; m_denominator = iDenominator; } 4 4

//Operator Overloading public static Fraction operator+(Fraction f1, Fraction f2) { Fraction Result = new Fraction( ); if( f1.Denominator != f2.Denominator ) { Result.Denominator = f1.Denominator * f2.Denominator; Result.Numerator = (f1.Numerator * f2.Denominator) + (f2.Numerator * f1.Denominator); } else { Result.Denominator = f1.Denominator; Result.Numerator = f1.Numerator + f2.Numerator; } return Result; } public static Fraction operator-(Fraction f1, Fraction f2) { Fraction Result = new Fraction( ); if( f1.Denominator != f2.Denominator ) { Result.Denominator = f1.Denominator * f2.Denominator; Result.Numerator = (f1.Numerator * f2.Denominator) - (f2.Numerator * f1.Denominator); } else { Result.Denominator = f1.Denominator; Result.Numerator = f1.Numerator - f2.Numerator; } return Result; } }//end of class Fraction 5 5

public class OperatorTest { public static void Main( ) { Fraction f1 = new Fraction( 1, 5 ); Fraction f2 = new Fraction( 2, 5 ); Fraction f3 = f1 + f2; Console.WriteLine("f1 + f2 = {0}/{1}", f3.Numerator, f3.Denominator ); f3 = f3 - f2; Console.WriteLine("f3 - f2 = {0}/{1}", f3.Numerator, f3.Denominator ); Console.ReadLine(); } 6 6

تعريف مجدد عملگرها تبديل نوع تبديل نوع صريح (explicit) نوع مشخص مي‌شود. تبديل نوع غير صريح يا ضمني (implicit) نوع مشخص نمي‌شود. Fraction f=new Fraction(1, 5); double d = f; //implicit cast double dd = (double)f; //explicit cast 7

فرم تابع تعريف مجدد عملگر : public static [implicit | explicit] operator return-type (Type T) 8

public static explicit operator double (Fraction f) { double dResult = ((double)f.Numerator / (double)f.Denominator); return dResult; } public static bool operator == (Fraction f1, Fraction f2) { if (f1.Numerator * f2.Denominator == f1.Denominator * f2.Numerator) return true; else return false; } public static bool operator != (Fraction f1, Fraction f2) { return !(f1 == f2); } 9 9

Download ppt "برنامه سازي پيشرفته 1 4. using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public."

Similar presentations