"Java Basics" setter and getter method

Source: Internet
Author: User

1 //The following code implements settings and obtains student names and scores. 2 classlesson5homework3 {4      Public Static voidMain (string[] args)5     {6Testcode tc=NewTestcode ();7Tc. Studenttest ();//calling the test class8     }9 }Ten classStudent One //set to get the student value and the Boolean type RN and RS that indicate whether the name and score values are correct respectively A { -     PrivateString name; -     Private intscore; the     Private BooleanRN; -     Private Booleanrs; -     //private values that provide outside access with setter and Getter methods -     /* + The format is: - getter (for obtaining): + [non-private modifier] field type get field name (first letter uppercase) () A     { at return field name; -     } - setter (for setting): - [non-private modifier] void set field name (first letter capitalized) (field type variable) -     { - field = variable; in     } -  to Exception: The setter method and the Is method are Boolean types.  + setter format is the same as above, is method only need to set programming is can, the following is an example.  -     */ the  *      Public voidSetName (String N)//the setter and getter method for name $     {Panax Notoginseng         if(n!= "") -         { theName=N; +SETRN (true); A         } the         Else +         { -System.out.println ("error! Name is error! "); $SETRN (false); $         } -     } -      PublicString getName () the     { -         returnname;Wuyi     } the  -      Public voidSetScore (intS//score Setter and getter method Wu     { -         if(s>=0&&s<=100) About         { $Score=s; -Setrs (true); -         } -         Else A         { +System.out.println ("error! Score is error! "); theSetrs (false); -         } $     } the      Public intGetscore () the     { the         returnscore; the     } -  in      Public voidSETRN (BooleanXn//Boolean setter method with the IS method the     { thern=xn; About     } the      Public BooleanISRN () the     { the         returnRN; +     } -  the      Public voidSetrs (Booleanxs)Bayi     { thers=xs; the     } -      Public BooleanisRs () -     { the         returnrs; the     } the } the classTestcode - { the      Public voidStudenttest ()//Test the     { theSystem.out.println ("TEST CODE of STUDENT:");94Printstudent ("Jack", 100); thePrintstudent ("Jack", 101); thePrintstudent ("Jack",-1); thePrintstudent ("", 100);98     } About     Private voidPrintstudent (String name,intscore) -     {101Student s=NewStudent ();102 s.setname (name);103S.setscore (score);//set the value first104         if((S.ISRN ()) && (S.isrs ()))//if correct, the output theSystem.out.println ("Name:" +s.getname () + ", Score:" +S.getscore ());106 107     }108}

I am a small rookie, the above for online self-study experience summary, if there are errors please point out. Novice children shoes A lot of exchanges, the great God of many guidance. I wish you all a happy life.

"Java Basics" setter and getter method

Related Article

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.