2015-12-15 21:26:17
Just learned Java to make a summary:
1. The constructor does not return a value.
The 2.A object calls the Q method, and the variable in the Q method is the variable of a.
Fraction Add (fraction f)
In this method is more obvious
The Red FM is the caller's FM.
The reason for this in this constructor is to be confused with a name in the argument
Of course
fraction (int fz1,int FM)//constructor method has no return type and has the same name as the class
{
FZ=FZ1;
if (fm==0)
This.fm=1;
Else
{
THIS.FM=FM;
}
}
It's not wrong to write like this.
Package Chapter2;public class fraction{ int FZ; int FM; fraction (int fz,int FM)//construction method has no return type and has the same name as the class { this.fz=fz; if (fm==0) this.fm=1; else { this.fm=fm; } } Fraction Add (fraction f) { int rfm,rfz; Rfm=fm*f.fm; RFZ=FZ*F.FM+F.FZ*FM; Fraction f3=new fraction (RFZ,RFM); return f3; } void display () { System.out.println (fz+ "/" +FM); } public static void Main (String arg[]) { fraction f1=new fraction (2,3); Fraction f2=new fraction (1,3); Fraction F3; F3=f1.add (F2); F3.display (); } }
Java Beginner (i)