生成代码:设计一个抽象类Shape,定义抽象方法getArea()和getPerimeter(),分别表示形状的面积和周长。然后设计两个子类Rectangle和Circle,分别继承Shape,并实现抽象方法。
时间: 2024-05-27 11:13:09 浏览: 55
Rectange类的代码如下:
abstract class Shape {
public abstract float getArea();
public abstract float getPerimeter();
}
class Rectangle extends Shape {
private float length;
private float width;
public Rectangle(float l, float w) {
length = l;
width = w;
}
public float getArea() {
return length * width;
}
public float getPerimeter() {
return 2 * (length + width);
}
}
而Circle类的代码如下:
class Circle extends Shape {
private float radius;
public Circle(float r) {
radius = r;
}
public float getArea() {
return (float) (Math.PI * radius * radius);
}
public float getPerimeter() {
return (float) (2 * Math.PI * radius);
}
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)