以下のコードでコンストラクタを実装できました
package shape;
public class Main {
public static void main(String[] args) {
Triangle tri = new Triangle("三角形", 12, 2);
tri.introduction();
System.out.println("Area = " + tri.getArea());
}
}
package shape;
public class Shape {
/** 図形名*/
String name;
/** コンストラクタ */
public Shape(String name){
this.name = name;
}
/** 図形名を表示するメソッド*/
public void introduction(){
System.out.println("これは" + name + "です");
}
}
package shape;
public class Triangle extends Shape{
private String name;
private int height;
private int bottom;
public Triangle(String name, int height, int bottom) {
super(name);
setName(name);
this.height = height;
this.bottom = bottom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getBottom() {
return bottom;
}
public void setBottom(int bottom) {
this.bottom = bottom;
}
public double getArea() {
return height * bottom * 0.5;
}
public void introduction() {
System.out.println("これは、" + getName() + "です");
System.out.println("面積は、" + height * bottom * 0.5 + "です");
}
}