1段階目(Bookクラスにコンストラクタを作ろう)

This commit is contained in:
s-sakiyama
2026-09-15 13:01:42 +09:00
parent 066af8799d
commit d4b07b24bf
2 changed files with 28 additions and 0 deletions

View File

@@ -10,6 +10,20 @@ public class BasicCheck1 {
System.out.println(book.title);
System.out.println(book.author);
System.out.println(book.price);
System.out.println();
//9/15段階1
Book book1 = new Book();
System.out.println(book1.title);
System.out.println(book1.author);
System.out.println(book1.price);
Book book2 = new Book("銀河鉄道の夜", "宮沢賢治", 500);
System.out.println(book2.title);
System.out.println(book2.author);
System.out.println(book2.price);
}
}

View File

@@ -3,4 +3,18 @@ public class Book {
String title;
String author;
int price;
//コンストラクタ
public Book() {
title = "タイトル未設定";
author = "著者未設定";
price = 0;
}
//引数ありコンストラクタ
public Book(String title, String author, int price) {
this.title = title;
this.author = author;
this.price = price;
}
}