The first method; object initialization.
PackageCom.hanqi;Importjava.lang.*; Public classstudent{intXuehao; String name; intAge ; Student (intXuehao,string name,intAge ) { This. xuehao=Xuehao; This. name=name; This. age=Age ; } voidoutput () {System.out.println ("School Number:" +xuehao+ "" + "Name:" +name+ "" + "Age:" +Age ); } Public Static voidMain (string[] args) {student S1=NewStudent (44, "Xiaoming", 21); S1.output (); }}
In the second method, you create multiple objects of the student class in the main method of the Master class, using these objects to measure
PackageCom.hanqi; Public classTextclass { Public Static voidMain (string[] args) {student S1=NewStudent (1, "Xiaoming", 21); S1.output (); Student S2=NewStudent (2, "Little Red", 22); S2.output (); Student S3=NewStudent (3, "Xiao Wen", 23); S3.output (); Student S4=NewStudent (4, "Little Fang", 24); S4.output (); }}
Java class and Object basics Exercise 1, student information, 2 ways