Once we have seen the common method of taking the parameters, we again pull the view into the depth of the parameter method.
In the method of the array as a parameter we know that we can add multiple students ' scores to the array and print them out.
So if now not only to add students ' grades, but also to add the student's age and performance, how to achieve it?
In the face of this problem we can use object-oriented thinking, all the student information to be added to the student class , only need to pass a student object in the method can contain all the information.
Let's take a look at how it should be done.
1 Public classStudent {2 //Student Class3 Public intID;4 PublicString name;5 Public intAge ;6 Public intscore;7 Public voidShowinfo () {8System.out.println (id+ "\ t" +name+ "\ T" +age+ "\ T" +score);9 }Ten One A Public classstudentsbiz { - /** - * Student Management class the */ -Student[] Students =NewStudent[30]; - - //Increase student + Public voidaddstudent (Student stu) { - for(inti = 0; i < students.length; i++) { + if(students[i]==NULL) { Astudents[i]=Stu; at Break; - } - } - } - - //Show student information in this class in Public voidshowstudents () { -SYSTEM.OUT.PRINTLN ("Student list of this class"); to for(inti = 0; i < students.length; i++) { + if(students[i]!=NULL) { - students[i].showinfo (); the } * } $ System.out.println ();Panax Notoginseng } - the + A Public Static voidMain (string[] args) { the /** + * Method of object as parameter - * Example 5. $ * Instantiate student objects and initialize them $ */ -Student Student1 =NewStudent (); -student1.id=10; theStudent1.name= "Wang er"; -Student1.age=18;Wuyistudent1.score=99; the -Student Student2 =NewStudent (); Wustudent2.id=11; -Student2.name= "Zhang San"; AboutStudent2.age=18; $student2.score=100; - - //New Student Objects -Studentsbiz biz =Newstudentsbiz (); A biz.addstudent (student1); + biz.addstudent (student2); the biz.showstudents (); -}
Object-oriented thinking to promote is not a temporary work, you need to practice!
An in-depth understanding of the method-----object as a parameter with the parameter method