Good morning. I'm new to JAVA. My question is about the definition of a method whose return value is of the same type as the class that is being defined. I really do not understand how this mechanism works.
I leave an extract of a class to show my doubt. Also I do not understand how you can define a Fraccion c=new Fraccion();
object within the class definition Fraccion
.
Within the methods we define Fraccion c=new Fraccion();
that is of the same type of the class that is being defined and the add and subtract methods that return a data of type Fraction.
public class Fraccion {
private int num;
private int den;
public Fraccion() {
num=0;
den=1;
}
public Fraccion(int x, int y) {
num=x;
den=y;
}
public static Fraccion sumar(Fraccion a, Fraccion b){
Fraccion c=new Fraccion();
c.num=a.num*b.den+b.num*a.den;
c.den=a.den*b.den;
return c;
}
public static Fraccion restar(Fraccion a, Fraccion b){
Fraccion c=new Fraccion();
c.num=a.num*b.den-b.num*a.den;
c.den=a.den*b.den;
return c;
}