java编写一个形状类,有计算面积、计算周长的方法。编写一个它的子类圆、长方形,并计算相应的周长和面积。
时间: 2023-09-21 13:06:17 浏览: 89
java用接口、多态、继承、类计算三角形和矩形周长及面积的方法
5星 · 资源好评率100%
### 回答1:
这是一个示例代码,你可以参考这个实现:
```java
public class Shape {
public double getArea() {
return 0;
}
public double getPerimeter() {
return 0;
}
}
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
@Override
public double getPerimeter() {
return 2 * (length + width);
}
}
```
你可以在主类中测试这两个子类的方法:
```java
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("Circle area: " + circle.getArea());
System.out.println("Circle perimeter: " + circle.getPerimeter());
Rectangle rectangle = new Rectangle(3, 4);
System.out.println("Rectangle area: " + rectangle.getArea());
System.out.println("Rectangle perimeter: " + rectangle.getPerimeter());
}
```
输出结果为:
```
Circle area: 78.53981633974483
Circle perimeter: 31.41592653589793
Rectangle area: 12.0
Rectangle perimeter: 14.0
```
### 回答2:
形状类是一个抽象的类,用来表示各种形状的基本属性和行为。它包含了两个方法:计算面积和计算周长。
首先,我们来编写形状类的代码:
```java
public abstract class Shape {
// 计算面积的方法
public abstract double calculateArea();
// 计算周长的方法
public abstract double calculatePerimeter();
}
```
接下来,我们可以编写一个子类圆,继承自形状类,并实现相应的面积和周长计算方法:
```java
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
```
然后,我们可以再编写一个子类长方形,同样继承自形状类,并实现相应的面积和周长计算方法:
```java
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
@Override
public double calculatePerimeter() {
return 2 * (length + width);
}
}
```
最后,我们可以通过创建圆和长方形对象,来计算它们的面积和周长:
```java
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5);
double circleArea = circle.calculateArea();
double circlePerimeter = circle.calculatePerimeter();
System.out.println("圆的面积:" + circleArea);
System.out.println("圆的周长:" + circlePerimeter);
Rectangle rectangle = new Rectangle(4, 6);
double rectangleArea = rectangle.calculateArea();
double rectanglePerimeter = rectangle.calculatePerimeter();
System.out.println("长方形的面积:" + rectangleArea);
System.out.println("长方形的周长:" + rectanglePerimeter);
}
}
```
以上就是用Java编写一个形状类及其子类圆和长方形,并计算它们的面积和周长的方法。
### 回答3:
形状类是一个抽象类,无法直接实例化。我们可以在形状类中定义抽象方法来计算面积和周长,同时创建一个圆类和长方形类继承形状类并实现这些方法。
首先,我们创建一个Shape类:
```java
public abstract class Shape {
public abstract double calculateArea();
public abstract double calculatePerimeter();
}
```
然后,我们创建一个Circle类,它是形状类的子类:
```java
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * Math.pow(radius, 2);
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
```
接下来,我们创建一个Rectangle类,也是形状类的子类:
```java
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
@Override
public double calculatePerimeter() {
return 2 * (length + width);
}
}
```
现在我们可以进行测试:
```java
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("圆的面积:" + circle.calculateArea());
System.out.println("圆的周长:" + circle.calculatePerimeter());
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("长方形的面积:" + rectangle.calculateArea());
System.out.println("长方形的周长:" + rectangle.calculatePerimeter());
}
}
```
输出结果如下:
```
圆的面积:78.53981633974483
圆的周长:31.41592653589793
长方形的面积:24.0
长方形的周长:20.0
```
至此,我们成功地通过Java编写了一个形状类及其子类圆和长方形,实现了计算面积和周长的方法。
阅读全文