写一个完整的Java Application程序,包含接口ShapeArea,以及MyRectangle类,MyTriangle类和Test类
时间: 2023-09-03 22:28:06 浏览: 139
### 回答1:
public interface ShapeArea {
double getArea();
}public class MyRectangle implements ShapeArea {
private double width;
private double height;
public MyRectangle(double width, double height) {
this.width = width;
this.height = height;
} public double getArea() {
return width * height;
}
}public class MyTriangle implements ShapeArea {
private double base;
private double height; public MyTriangle(double base, double height) {
this.base = base;
this.height = height;
} public double getArea() {
return 0.5 * base * height;
}
}public class Test {
public static void main(String[] args) {
MyRectangle myRectangle = new MyRectangle(4, 5);
MyTriangle myTriangle = new MyTriangle(4, 5);
System.out.println("长方形面积:" + myRectangle.getArea());
System.out.println("三角形面积:" + myTriangle.getArea());
}
}
### 回答2:
接口ShapeArea定义:
```java
public interface ShapeArea {
double getArea();
}
```
MyRectangle类定义:
```java
public class MyRectangle implements ShapeArea {
private double length;
private double width;
public MyRectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
}
```
MyTriangle类定义:
```java
public class MyTriangle implements ShapeArea {
private double base;
private double height;
public MyTriangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
}
```
Test类定义:
```java
public class Test {
public static void main(String[] args) {
MyRectangle rectangle = new MyRectangle(5, 7);
MyTriangle triangle = new MyTriangle(4, 6);
System.out.println("Rectangle area: " + rectangle.getArea());
System.out.println("Triangle area: " + triangle.getArea());
}
}
```
这个Java应用程序定义了一个ShapeArea接口,以及MyRectangle类和MyTriangle类实现了该接口,并重写了getArea()方法计算矩形和三角形的面积。Test类用于测试这些类的功能,创建了一个矩形对象和一个三角形对象,并输出它们的面积。运行该程序将会打印出矩形和三角形的面积。
### 回答3:
接口ShapeArea:
```java
public interface ShapeArea {
double getArea();
}
```
MyRectangle类:
```java
public class MyRectangle implements ShapeArea {
private double width;
private double height;
public MyRectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}
```
MyTriangle类:
```java
public class MyTriangle implements ShapeArea {
private double base;
private double height;
public MyTriangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
}
```
Test类:
```java
public class Test {
public static void main(String[] args) {
MyRectangle rectangle = new MyRectangle(5, 10);
MyTriangle triangle = new MyTriangle(4, 6);
System.out.println("矩形的面积为:" + rectangle.getArea());
System.out.println("三角形的面积为:" + triangle.getArea());
}
}
```
通过上述代码,我们定义了一个接口ShapeArea,该接口包含一个计算图形面积的方法getArea。然后我们分别实现了MyRectangle类和MyTriangle类,这两个类都实现了ShapeArea接口,并在getArea方法中分别计算了矩形和三角形的面积。最后在Test类中创建了一个MyRectangle对象和一个MyTriangle对象,并通过调用它们的getArea方法打印出它们的面积。
阅读全文