php定义一个矩形类rectangle,c#定义一个类圆Circle或者定义一个矩形类Rectangle,分别计算它们的周长和面积....
时间: 2024-03-23 16:40:52 浏览: 103
定义一个名为rectangle 的矩形类,其属性数据为矩形左上角和右上角的点的坐标能计算矩形的面积
5星 · 资源好评率100%
PHP定义一个矩形类Rectangle:
```php
class Rectangle {
private $length;
private $width;
public function __construct($length, $width) {
$this->length = $length;
$this->width = $width;
}
public function getArea() {
return $this->length * $this->width;
}
public function getPerimeter() {
return 2 * ($this->length + $this->width);
}
}
```
C#定义一个类圆Circle:
```csharp
class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double GetArea() {
return Math.PI * radius * radius;
}
public double GetPerimeter() {
return 2 * Math.PI * radius;
}
}
```
C#定义一个矩形类Rectangle:
```csharp
class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double GetArea() {
return length * width;
}
public double GetPerimeter() {
return 2 * (length + width);
}
}
```
以上是计算矩形和圆的面积和周长的示例代码。
阅读全文