2日目3段階目完了しました。
This commit is contained in:
@@ -3,12 +3,19 @@ public class BasicCheck2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
Student student = new Student();
|
||||
|
||||
student.studentId=1;
|
||||
student.name="鈴木一郎";
|
||||
|
||||
student.printProfile();
|
||||
//コンストラクタを使ってインスタンスを作成
|
||||
Student student1 = new Student(1,"鈴木一郎");
|
||||
Student student2 = new Student(2,"田中花子");
|
||||
Student student3 = new Student(3,"佐藤次郎");
|
||||
|
||||
//メソッド呼び出し
|
||||
student1.printProfile();
|
||||
student2.printProfile();
|
||||
student3.printProfile();
|
||||
|
||||
//クラスから直接呼び出し
|
||||
System.out.println("現在の生徒数" +Student.getStudentCount() + "人");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,23 @@
|
||||
|
||||
public class Student {
|
||||
//変数を作る
|
||||
int studentId;
|
||||
String name;
|
||||
|
||||
//フィールド追加
|
||||
static int studentCount = 0;
|
||||
//コンストラクタ追加
|
||||
public Student(int studentId,String name) {
|
||||
this.studentId = studentId;
|
||||
this.name = name;
|
||||
studentCount++;
|
||||
}
|
||||
|
||||
//メソッド追加
|
||||
public static int getStudentCount() {
|
||||
return studentCount;
|
||||
}
|
||||
//メソッド
|
||||
public void printProfile() {
|
||||
System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user