Java class and object 3, java object 3

Source: Internet
Author: User

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:

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.