Day3課題2
q5の1~3まで終了
This commit is contained in:
@@ -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("円"); //"円"呼び出し 文字列
|
||||
|
||||
@@ -7,5 +7,8 @@ public class Day3Check1 {
|
||||
circle.area();
|
||||
circle.showInfo();
|
||||
|
||||
// Rectangle rectangle = new Rectangle(2);
|
||||
// rectangle.area();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
22
em-oop/src/Day3Check2.java
Normal file
22
em-oop/src/Day3Check2.java
Normal file
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
15
em-oop/src/Day3Check3.java
Normal file
15
em-oop/src/Day3Check3.java
Normal file
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
4
em-oop/src/Greeting.java
Normal file
4
em-oop/src/Greeting.java
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
public interface Greeting {
|
||||
String hello();
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class Item {
|
||||
//メソッドを宣言
|
||||
|
||||
//stockからamountを引く
|
||||
this.getStock(this.getStock() - amount);
|
||||
// this.getStock(this.getStock() - amount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
13
em-oop/src/JapaneseGreeting.java
Normal file
13
em-oop/src/JapaneseGreeting.java
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
//JaGrクラスを作る、implements GrによってGreetingインターフェースを実装する
|
||||
public class JapaneseGreeting implements Greeting {
|
||||
|
||||
//Grインターフェースで定義されているhello()メソッドをこのクラスに実装する
|
||||
@Override
|
||||
public String hello() {
|
||||
//hello()が呼び出されたらString型の文字列を呼び出し元に返す
|
||||
return "こんにちは";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
22
em-oop/src/Rectangle.java
Normal file
22
em-oop/src/Rectangle.java
Normal file
@@ -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 + ")");
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user