Java class and object 3, java object 3
Write Java applications. First, define the Student class-Student, including Student ID (int), name (String), age (int) and other attributes; two methods: Student (int stuNo, String name, int age)
It is used to initialize objects, and outPut () is used to outPut student information. Next, define a main class-TestClass, create multiple Student class objects in the main method of the main class, and use these objects to test the function of the Student class.
1 // create Class 2 int stuNo; 3 String stuName; 4 int stuAge; 5 // constructor 6 student (int stuNo, String name, int age) 7 {8 this. stuNo = stuNo; 9 stuName = name; 10 stuAge = age; 11} 12 void outPut () 13 {14 System. out. println (stuName + "\ t" + stuNo + "\ t" + stuAge); 15} 16 17 public static void main (String [] args) {18 student xs1 = new student (10001, "Zhang San", 25); 19 student xs2 = new student (10002, "Li Si", 27 ); 20 student xs3 = new student (10003, "Wang Wu", 22); 21 student xs4 = new student (10004, "Ma ", 19 ); 22 student xs5 = new student (10005, "Feng Qi", 22); 23 student xs6 = new student (10006, "Qin ba", 26); 24 25 System. out. println ("Name: \ t student ID: \ t age:"); 26 xs1.outPut (); 27 xs2.outPut (); 28 xs3.outPut (); 29 xs4.outPut (); 30 xs5.outPut (); 31 xs6.outPut (); 32}
Running result: