1.定义接口AreaInterface,该接口有一个double类型常量PI,它的值等于Math. PI;含有一个求面积的方法double area() 2.定义一个长方形,实现Areainterface接口,该类有两个private访问权限的float变量x(长)和y(宽);定义一个构造方法,用来给类变量赋值,实现area()方法得到长方形的面积,重写toString方法,返回一段字符串信息,内容如下格式:“该长方形面积为:+面积” 3.定义一个Testarea为主类,创建一个Recctangle的实例,长为10.0,宽为20.0,输出它的面积
时间: 2023-10-04 19:05:28 浏览: 58
1. AreaInterface接口的定义如下:
```
public interface AreaInterface {
double PI = Math.PI;
double area();
}
```
2. Rectangle类的定义如下:
```
public class Rectangle implements AreaInterface {
private float x; // 长
private float y; // 宽
public Rectangle(float x, float y) {
this.x = x;
this.y = y;
}
@Override
public double area() {
return x * y;
}
@Override
public String toString() {
return "该长方形面积为:" + area();
}
}
```
3. Testarea类的定义如下:
```
public class Testarea {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10.0f, 20.0f);
System.out.println(rectangle.toString());
}
}
```
输出结果为:
```
该长方形面积为:200.0
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)