java鎬庝箞鍒涘缓瀵硅薄鏁扮粍
鍦↗ava涓紝鍙互閫氳繃浠ヤ笅姝ラ鏉ュ垱寤哄璞℃暟缁勶細
- 棣栧厛锛岄渶瑕佸畾涔変竴涓被鏉ヨ〃绀烘暟缁勪腑鐨勫璞°€備緥濡傦紝鍋囪鎴戜滑瑕佸垱寤轰竴涓瓨鍌ㄥ鐢熷璞$殑鏁扮粍锛屽彲浠ュ畾涔変竴涓悕涓篠tudent鐨勭被銆傜被鐨勭ず渚嬩唬鐮佸涓嬶細
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// 鐪佺暐鍏朵粬鏂规硶鍜屽睘鎬х殑瀹氫箟
}
- 鍒涘缓瀵硅薄鏁扮粍涔嬪墠锛岄渶瑕佸厛纭畾鏁扮粍鐨勫ぇ灏忋€傚彲浠ヤ娇鐢ㄤ互涓嬭娉曞垱寤轰竴涓寚瀹氬ぇ灏忕殑瀵硅薄鏁扮粍锛?/li>
Student[] students = new Student[5]; // 鍒涘缓鍖呭惈5涓厓绱犵殑瀵硅薄鏁扮粍
- 鍒涘缓瀵硅薄鏁扮粍鍚庯紝鍙互閫氳繃浠ヤ笅璇硶涓烘瘡涓暟缁勫厓绱犺祴鍊硷細
students[0] = new Student("Alice", 18);
students[1] = new Student("Bob", 19);
students[2] = new Student("Charlie", 20);
students[3] = new Student("Dave", 21);
students[4] = new Student("Eve", 22);
杩欐牱灏卞垱寤轰簡涓€涓寘鍚?涓鐢熷璞$殑鏁扮粍銆?/p>
瀹屾暣绀轰緥浠g爜濡備笅锛?/p>
public class Main {
public static void main(String[] args) {
Student[] students = new Student[5]; // 鍒涘缓鍖呭惈5涓厓绱犵殑瀵硅薄鏁扮粍
students[0] = new Student("Alice", 18);
students[1] = new Student("Bob", 19);
students[2] = new Student("Charlie", 20);
students[3] = new Student("Dave", 21);
students[4] = new Student("Eve", 22);
// 閬嶅巻鏁扮粍骞惰緭鍑烘瘡涓鐢熺殑淇℃伅
for (Student student : students) {
System.out.println("Name: " + student.getName() + ", Age: " + student.getAge());
}
}
}
杩愯浠ヤ笂浠g爜灏嗚緭鍑猴細
Name: Alice, Age: 18
Name: Bob, Age: 19
Name: Charlie, Age: 20
Name: Dave, Age: 21
Name: Eve, Age: 22