Java Beginner (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.