diff --git a/em-oop/src/Circle.java b/em-oop/src/Circle.java index b949cb9..443185b 100644 --- a/em-oop/src/Circle.java +++ b/em-oop/src/Circle.java @@ -1,8 +1,8 @@ public class Circle extends Shape { //shapeクラスを継承する - private double radius; { //radius: 半径 フィールドradius (double型、private) + private double radius; //radius: 半径 フィールドradius (double型、private) - } + public Circle(double radius) { //コンストラクタpub 引数dou/raを受け取る super("円"); //"円"呼び出し 文字列 diff --git a/em-oop/src/Day3Check1.java b/em-oop/src/Day3Check1.java index 253263b..6682a59 100644 --- a/em-oop/src/Day3Check1.java +++ b/em-oop/src/Day3Check1.java @@ -7,5 +7,8 @@ public class Day3Check1 { circle.area(); circle.showInfo(); +// Rectangle rectangle = new Rectangle(2); +// rectangle.area(); + } } diff --git a/em-oop/src/Day3Check2.java b/em-oop/src/Day3Check2.java new file mode 100644 index 0000000..53fca92 --- /dev/null +++ b/em-oop/src/Day3Check2.java @@ -0,0 +1,22 @@ + +public class Day3Check2 { + public static void main(String[] args) { + + //Rect-クラスをもとにrect-のインスタンスつくる + //3,4をRectangleのコンストラクタに渡す + Rectangle rectangle = new Rectangle(3, 4); + + //Rect-はShapeを継承しているため親クラスのshowInfoメソッドを使える + rectangle.showInfo(); + + + //JaGrクラスをもとにインスタンスをつくる + //変数の型はGreetingインタフェース型にしている + Greeting greeting = new JapaneseGreeting(); + + //greeetingのhelloメソッドよびだす + //実際に入っているインスタンスはJaGrなのでJaGrで作ったhello()が実行される + System.out.println(greeting.hello()); + } + +} diff --git a/em-oop/src/Day3Check3.java b/em-oop/src/Day3Check3.java new file mode 100644 index 0000000..3a177e4 --- /dev/null +++ b/em-oop/src/Day3Check3.java @@ -0,0 +1,15 @@ + +public class Day3Check3 { + public static void main(String[] args) { + + //Shape型の変数にCircleのインスタンスを代入 + Shape shape1 = new Circle(2); + //円の情報を表示 + shape1.showInfo(); + //Shape型の変数にRectangleのインスタンスを代入 + Shape shape2 = new Rectangle(3, 4); + //四角形の情報を表示 + shape2.showInfo(); + + } +} diff --git a/em-oop/src/Greeting.java b/em-oop/src/Greeting.java new file mode 100644 index 0000000..3affaf3 --- /dev/null +++ b/em-oop/src/Greeting.java @@ -0,0 +1,4 @@ + +public interface Greeting { + String hello(); +} diff --git a/em-oop/src/Item.java b/em-oop/src/Item.java index d710c22..57a6a61 100644 --- a/em-oop/src/Item.java +++ b/em-oop/src/Item.java @@ -37,7 +37,7 @@ public class Item { //メソッドを宣言 //stockからamountを引く - this.getStock(this.getStock() - amount); +// this.getStock(this.getStock() - amount); } } diff --git a/em-oop/src/JapaneseGreeting.java b/em-oop/src/JapaneseGreeting.java new file mode 100644 index 0000000..2bb96a3 --- /dev/null +++ b/em-oop/src/JapaneseGreeting.java @@ -0,0 +1,13 @@ + +//JaGrクラスを作る、implements GrによってGreetingインターフェースを実装する +public class JapaneseGreeting implements Greeting { + + //Grインターフェースで定義されているhello()メソッドをこのクラスに実装する + @Override + public String hello() { + //hello()が呼び出されたらString型の文字列を呼び出し元に返す + return "こんにちは"; + + } + +} diff --git a/em-oop/src/Rectangle.java b/em-oop/src/Rectangle.java new file mode 100644 index 0000000..d56e86b --- /dev/null +++ b/em-oop/src/Rectangle.java @@ -0,0 +1,22 @@ + +public class Rectangle extends Shape { + private double width; + private double height; { + + } + + public Rectangle(double width, double height) { + super("四角形"); + this.width = width; + this.height = height; + } + + public double area() { + return width * height; + } +// public void showInfo(){ +// super.showInfo(); +// System.out.println("(半径:" + this.area + ")"); +// } + +} diff --git a/em-oop/src/Shape.java b/em-oop/src/Shape.java index 790e1bd..9ca0280 100644 --- a/em-oop/src/Shape.java +++ b/em-oop/src/Shape.java @@ -1,9 +1,10 @@ -public class Shape { +public abstract class Shape { private String name; //フィールドname ・privateにしなきゃいけない - public double area; - public int radius; +// public double area; +// public int radius; + public Shape() { } @@ -12,8 +13,8 @@ public class Shape { this.name = name; //Circleが"円"を呼び出すための文字列をいれるところ } - public double area() { //メソッドarea 引数なし・戻り値double 中身return0.0; - return 0.0; + public abstract double area();{ //メソッドarea 引数なし・戻り値double 中身return0.0; +// return 0.0; } // @Override @@ -21,7 +22,8 @@ public class Shape { public void showInfo(){ //メソッドshowinfo 引数戻り値なし・中身はsyso System.out.println(this.name + "の面積は" + this.area() + "です"); - System.out.println("(半径:" + this.radius + ")"); +// System.out.println("(半径:" + this.radius + ")"); + System.out.println("四角形" + "の面積は" + this.area() + "です。"); } // @Override