From c82f9ca678890083300b1d9f8b946dbad45ddf98 Mon Sep 17 00:00:00 2001 From: a-takayama <26-a-takayama-scm@it-college.co.jp> Date: Tue, 15 Sep 2026 14:36:27 +0900 Subject: [PATCH] =?UTF-8?q?2=E6=97=A5=E7=9B=AE=EF=BC=BF=E6=AE=B5=E9=9A=8E1?= =?UTF-8?q?=EF=BC=BF=E5=AE=8C=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- em-oop/src/BasicCheck1.java | 21 +++++++++++++-------- em-oop/src/BasicCheck2.java | 6 +++--- em-oop/src/BasicCheck3.java | 8 ++++++++ em-oop/src/Book.java | 23 ++++++++++++++++++++--- em-oop/src/Item.java | 11 +++++++++++ em-oop/src/Student.java | 20 ++++++++++---------- 6 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 em-oop/src/BasicCheck3.java create mode 100644 em-oop/src/Item.java diff --git a/em-oop/src/BasicCheck1.java b/em-oop/src/BasicCheck1.java index f62f54e..de49a69 100644 --- a/em-oop/src/BasicCheck1.java +++ b/em-oop/src/BasicCheck1.java @@ -3,17 +3,22 @@ public class BasicCheck1 { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ - Book book = new Book(); - book.title = "吾輩は猫である"; - book.author ="夏目漱石"; - book.price = 800; +// Book book = new Book(); +// book.title = "吾輩は猫である"; +// book.author ="夏目漱石"; +// book.price = 800; - System.out.println(book.title); - System.out.println(book.author); - System.out.println(book.price); + 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); - } diff --git a/em-oop/src/BasicCheck2.java b/em-oop/src/BasicCheck2.java index 3855268..ee5fb4e 100644 --- a/em-oop/src/BasicCheck2.java +++ b/em-oop/src/BasicCheck2.java @@ -1,11 +1,11 @@ public class BasicCheck2 { public static void main(String[] args) { - Student student = new Student(1,"鈴木一郎"); - + Student student = new Student(); +// student.studentId = 1; student.name = "鈴木一郎"; - +// // System.out.print("学籍番号"+student.studentId); // System.out.print("名前:" + student.name); student.printProfile(); diff --git a/em-oop/src/BasicCheck3.java b/em-oop/src/BasicCheck3.java new file mode 100644 index 0000000..07cda80 --- /dev/null +++ b/em-oop/src/BasicCheck3.java @@ -0,0 +1,8 @@ + +public class BasicCheck3 { + public static void main() { + Item[] items = new Item[3]; + + } + +} diff --git a/em-oop/src/Book.java b/em-oop/src/Book.java index beb3c69..b9c888e 100644 --- a/em-oop/src/Book.java +++ b/em-oop/src/Book.java @@ -1,7 +1,24 @@ public class Book { - String title; - String author; - int price; + public String title; + public String author; + public int price; + + //引数なしコンストラクタ + public Book() { + this.title = "タイトル未設定"; + this.author = "著者未設定"; + this.price = 0; + +// System.out.println("コンストラクタが呼び出されました"); + + } + //引数ありコンストラクタ + public Book(String title, String author, int price) { +// System.out.println("引数なしのコンストラクタが呼び出されました"); + this.title = title; + this.author = author; + this.price = price; + } } diff --git a/em-oop/src/Item.java b/em-oop/src/Item.java new file mode 100644 index 0000000..e102633 --- /dev/null +++ b/em-oop/src/Item.java @@ -0,0 +1,11 @@ + +public class Item { + String name; + int price; + int stock; + + public void sell(int amount) { + this.stock = this.stock - amount; + } + +} diff --git a/em-oop/src/Student.java b/em-oop/src/Student.java index 307bffb..775b09c 100644 --- a/em-oop/src/Student.java +++ b/em-oop/src/Student.java @@ -1,22 +1,22 @@ public class Student { + int studentId; String name; - public Student(int studentId, String name) { - this.studentId = studentId; - this.name = name; - } +// public Student(int studentId, String name) { +// this.studentId = studentId; +// this.name; - public void printProfile() { + void printProfile() { System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name); } - public static void main(String[] args) { - Student s = new Student(1,"鈴木一郎"); - s.printProfile(); - +// public static void main(String[] args) { +// Student s = new Student(1,"鈴木一郎"); +// s.printProfile(); +// } -} +