1段階目(Bookクラスにコンストラクタを作ろう)
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user