3段階目(Studentクラスにコンストラクタとstaticメンバを追加しよう)
This commit is contained in:
@@ -3,10 +3,13 @@ public class BasicCheck2 {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO 自動生成されたメソッド・スタブ
|
// TODO 自動生成されたメソッド・スタブ
|
||||||
Student student = new Student();
|
Student student1 = new Student(1, "鈴木一郎");
|
||||||
student.studentId = 1;
|
Student student2 = new Student(2, "田中花子");
|
||||||
student.name = "鈴木一郎";
|
Student student3 = new Student(3, "佐藤次郎");
|
||||||
student.printProfile();
|
student1.printProfile();
|
||||||
|
student2.printProfile();
|
||||||
|
student3.printProfile();
|
||||||
|
System.out.println("現在の生徒数:" + Student.getStudentCount() + "人");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,19 @@
|
|||||||
public class Student {
|
public class Student {
|
||||||
int studentId;
|
int studentId;
|
||||||
String name;
|
String name;
|
||||||
|
static int studentCount = 0;
|
||||||
|
|
||||||
void printProfile() {
|
void printProfile() {
|
||||||
System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name);
|
System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Student(int studentId, String name) {
|
||||||
|
this.studentId = studentId;
|
||||||
|
this.name = name;
|
||||||
|
studentCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getStudentCount() {
|
||||||
|
return studentCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user