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