ProgrammingC#3.0-3-of1.pptVIP

  • 3
  • 0
  • 约2.69万字
  • 约 42页
  • 2016-11-28 发布于河南
  • 举报
ProgrammingC#3.0-3-of1

* It is quite common to overload the equality operator(==) to test whether two objects are equal(how ever equality might be defined for your object).C# insists that if you overload the equals(==) operator, you must also overload the not-equals operator (!=).Similarly,the less-than()and greater-than() operators must be paired, as must the less-than or equals (=) and greater-than or equals (=) operators. * Chapter 5. Nesting Classes Example of Nested class * * namespace Nested_Class { public class Fraction { private int numerator; private int denominator; public Fraction(int numerator, int denominator){…} internal class FractionArtist //?嵌套类的定义 { public void Draw(Fraction f) { … } } } //嵌套类的实例化,Fraction如同namespace Fraction.FractionArtist fa = new Fraction.FractionArtist(); fa.Draw(f1); Ch. 6 Operator Overloading 天津大学软件学院 章亦葵 email:zhangyk@ /faculty/zyksoft/ * * Chapter 6. Operator Overloading Introduction Adding standard operators to user defined types. Adding conversion operators to convert user defined types implicitly and explicitly to other types. implicitly : Fraction frdata = 5. * * Fraction theSum = firstFraction.Add(secondFraction) Fraction theSum = firstFraction + secondFraction Chapter 6. Operator Overloading Using the operator Keyword Creating static method to implement operators. Method return values represent the operation result and parameters are operands(操作数) * * public static user-type operator op (user-type lhs, user-type rhs) user-type result = lhs op rhs 实现重载 操作运算的返回值 要重载的 操作符 操作符左边的操作数 操作符右边的操作数 Chapter 6. Operator Overloading Example using operator keyword Suppose uesr type is Fraction, the operation is the sum(+) of two Faction operands. The operator overloading method is: * * theSum = lhs + rhs public static Fraction operator + (Fraction lhs, Fraction rhs) Chapter 6. Operator Overloading Sup

文档评论(0)

1亿VIP精品文档

相关文档