2段階目(抽象クラスとインターフェースを使ってみよう)
This commit is contained in:
14
em-oop/src/Day3Check2.java
Normal file
14
em-oop/src/Day3Check2.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
public class Day3Check2 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Rectangle rectangle = new Rectangle(3, 4);
|
||||||
|
rectangle.showInfo();
|
||||||
|
Greeting jpGreeting = new JapaneseGreeting();
|
||||||
|
System.out.println(jpGreeting.hello());
|
||||||
|
LoudGreeting jpShout = new JapaneseGreeting();
|
||||||
|
System.out.println(jpShout.shout());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
14
em-oop/src/JapaneseGreeting.java
Normal file
14
em-oop/src/JapaneseGreeting.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
public class JapaneseGreeting implements Greeting, LoudGreeting {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String hello() {
|
||||||
|
return "こんにちは";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String shout() {
|
||||||
|
return "やっほー!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
4
em-oop/src/LoudGreeting.java
Normal file
4
em-oop/src/LoudGreeting.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
public interface LoudGreeting {
|
||||||
|
String shout();
|
||||||
|
}
|
||||||
18
em-oop/src/Rectangle.java
Normal file
18
em-oop/src/Rectangle.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
public class Rectangle extends Shape {
|
||||||
|
|
||||||
|
private double width;
|
||||||
|
private double height;
|
||||||
|
|
||||||
|
public Rectangle(double width, double height) {
|
||||||
|
super("四角形");
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double area() {
|
||||||
|
return width * height;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
|
|
||||||
public class Shape {
|
public abstract class Shape {
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public Shape(String name) {
|
public Shape(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double area() {
|
public abstract double area();
|
||||||
return 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showInfo() {
|
public void showInfo() {
|
||||||
System.out.println(this.name + "の面積は" + this.area() + "です");
|
System.out.println(this.name + "の面積は" + this.area() + "です");
|
||||||
|
|||||||
Reference in New Issue
Block a user