An in-depth understanding of the method-----object as a parameter with the parameter method

Source: Internet
Author: User

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

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.