天天看點

java建立5個類的對象_java 面像對象中, 定義一個學生類,可以建立五個學生對象,自動産生...

展開全部

import java.util.Random;

public class StudentTest {

private int number;//學生學号

private String name;//學生名稱32313133353236313431303231363533e78988e69d8331333363353738

private int age;//學生年齡

public int getNumber() {

return number;

}

public void setNumber(int number) {

this.number = number;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public static void main(String[] args){

//第一種自動産生學号方法:用for循環,從一開始,一一放進去對象學生學号那裡

StudentTest[] student=new StudentTest[5];//五個對象數組

for(int i=0;i

student[i]=new StudentTest();//new對象

student[i].setNumber(i+1);//循環放入學号,

System.out.println(student[i].getNumber());

}

System.out.println("第二種方法");

//第二種方法,随機生成學号(前提是整數)

StudentTest[] student2=new StudentTest[5];//五個對象數組

for(int i=0;i

student2[i]=new StudentTest();//new對象

student2[i].setNumber((int)(Math.random()*100));//循環放入學号,

System.out.println(student2[i].getNumber());

}

//第三種方法,如果存在資料庫中。在資料庫表設定為自增屬性

}

}

執行結果如下:

java建立5個類的對象_java 面像對象中, 定義一個學生類,可以建立五個學生對象,自動産生...