刚开始学,这个怎么用java语言定义Student类(包括姓名、学号、籍贯、出生日期、专业等属性,获取姓名、学号、籍贯、年龄和专业的实例方法,增加initStudents(int number)静态方法,返回指定数量同学的数组,学号随机生成)
如有帮助,请点击采纳哦
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 定义Student类(包括姓名、学号、籍贯、出生日期、专业等属性,获取姓名、学号、籍贯、年龄和专业的实例方法,
* 增加initStudents(int number)静态方法,返回指定数量同学的数组,学号随机生成)
*/
public class Student {
private String no;
private String name;
private String nativePlace;
private Date birthday;
private String professional;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNativePlace() {
return nativePlace;
}
public void setNativePlace(String nativePlace) {
this.nativePlace = nativePlace;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getProfessional() {
return professional;
}
public void setProfessional(String professional) {
this.professional = professional;
}
@Override
public String toString() {
return "Student{" +
"no='" + no + '\'' +
", name='" + name + '\'' +
", nativePlace='" + nativePlace + '\'' +
", birthday=" + birthday +
", professional='" + professional + '\'' +
'}';
}
public static List<Student> initStudents(int number) {
List<Student> students = new ArrayList<>(number);
List<String> noList = new ArrayList<>();
while (students.size() < number) {
Student student = new Student();
String no = assembleNo();
if (!noList.contains(no)) {
noList.add(no);
student.setNo(no);
student.setName("学生" + students.size());
student.setProfessional("专业" + students.size());
students.add(student);
}
}
return students;
}
public static String assembleNo() {
int len = 6;
StringBuilder no = new StringBuilder();
for (int i = 0; i < len; i++) {
no.append((int) (Math.random() * 10));
}
return no.toString();
}
public static void main(String[] args) {
List<Student> students = initStudents(6);
for (Student student : students) {
System.out.println(student.toString());
}
}
}