In-depth understanding of the method with parameters ----- object as a parameter, in-depth understanding -----

Source: Internet
Author: User

In-depth understanding of the method with parameters ----- object as a parameter, in-depth understanding -----

After learning about the common parameter-based method, we will go into the perspective of the parameter-based method.

In the array as the parameter method, we know that we can add the scores of multiple students to the array and print them out,

How can we achieve this if we need to add not only the scores of students, but also the age and scores of students?

In the face of such problems, we can use the object-oriented thinking to encapsulate all the student information to be added into the student class, you only need to pass a student object in the method to include all the information.

Next, let's take a look at how to implement it.

1 public class Student {2 // Student class 3 public int id; 4 public String name; 5 public int age; 6 public int score; 7 public void showinfo () {8 System. out. println (id + "\ t" + name + "\ t" + age + "\ t" + score ); 9} 10 11 12 public class StudentsBiz {13/** 14 * Student management class 15 */16 Student [] students = new Student [30]; 17 18 // Add Student 19 public void addstudent (Student stu) {20 for (int I = 0; I <students. length; I ++) {21 if (studen Ts [I] = null) {22 students [I] = stu; 23 break; 24} 25} 26} 27 28 // display the student information of this class 29 public void showStudents () {30 System. out. println ("student list in this class"); 31 for (int I = 0; I <students. length; I ++) {32 if (students [I]! = Null) {33 students [I]. showinfo (); 34} 35} 36 System. out. println (); 37} 38 39 40 41 public static void main (String [] args) {42/** 43 * method of object as parameter 44 * example 5.45 * instantiate Student object and initialize 46 */47 Student student1 = new Student (); 48 student1.id = 10; 49 student1.name = ""; 50 student1.age = 18; 51 student1.score = 99; 52 53 Student student2 = new Student (); 54 student2.id = 11; 55 student2.name = "Zhang San "; 56 student2.age = 18; 57 student2.score = 100; 58 59 // Add students 60 StudentsBiz biz = new StudentsBiz (); 61 biz. addstudent (student1); 62 biz. addstudent (student2); 63 biz. showStudents (); 64}

 

It is not a temporary task to improve the object-oriented thinking. It is necessary to work diligently!

 

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.