定义父类class Shape{ String name; public void showName(){...........} } 2. 定义子类:圆形,三角形,矩形,包含求周长,求面积等公式; 3. 定义一个Main类,在其main函数中,用这三个子类创建对象,求其面积,周长等; 4. 请在Main类中定义参数为Shape类型的函数: static void showArea(Shape shape) {.............} static void showPerimeter(Shape shape){..............} 将三角形,圆形,矩形的对象传入,理解面向对象的深刻含义;
时间: 2023-06-10 17:09:14 浏览: 61
下面是实现上述要求的Java代码:
```
class Shape {
String name;
public void showName() {
System.out.println("图形名称:" + name);
}
}
class Circle extends Shape {
double radius;
public Circle(String name, double radius) {
this.name = name;
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
class Triangle extends Shape {
double a, b, c;
public Triangle(String name, double a, double b, double c) {
this.name = name;
this.a = a;
this.b = b;
this.c = c;
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
return a + b + c;
}
}
class Rectangle extends Shape {
double length, width;
public Rectangle(String name, double length, double width) {
this.name = name;
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
public double getPerimeter() {
return 2 * (length + width);
}
}
public class Main {
public static void showArea(Shape shape) {
System.out.println(shape.name + "的面积为:" + shape.getArea());
}
public static void showPerimeter(Shape shape) {
System.out.println(shape.name + "的周长为:" + shape.getPerimeter());
}
public static void main(String[] args) {
Circle circle = new Circle("圆形", 5);
Triangle triangle = new Triangle("三角形", 3, 4, 5);
Rectangle rectangle = new Rectangle("矩形", 4, 6);
showArea(circle);
showPerimeter(circle);
circle.showName();
showArea(triangle);
showPerimeter(triangle);
triangle.showName();
showArea(rectangle);
showPerimeter(rectangle);
rectangle.showName();
}
}
```
在上面的代码中,Shape是一个抽象类,用来定义图形类的通用属性和方法。Circle、Triangle和Rectangle都是Shape的子类,分别实现了自己的求面积和周长的方法。在Main类中,我们定义了静态方法showArea和showPerimeter,用来分别计算图形的面积和周长并输出。我们通过创建Circle、Triangle和Rectangle对象并传入这两个方法中,来展示面向对象编程的特点。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)