22.1. Explaining operator overloading
In both Java and C#, the binary * operator has always been used to obtain the product of two numerical values, which are the two operands. The code below shows a class which represents a fraction with two int fields – numerator and denominator. I have added a constructor which takes in two ints to initialize these two fields:
1: class Fraction{ 2: public int numerator; 3: public int denominator; 4: 5: // constructor 6: public Fraction (int numerator, int denominator){ 7: this.numerator = numerator; 8: this.denominator = denominator; 9: } 10: }
When you want to perform a fraction multiplication, you can write a static method, called Multiply, which takes in two Fractions and which returns a resultant new ...
Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.