using System; namespace RectangleApplication { class Rectangle { // 成员变量 double length; double width; public void Acceptdetails() { length = 4.5; width = 3.5; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); r.Display(); Console.ReadLine(); } } }详细解释
时间: 2023-06-14 14:02:54 浏览: 66
这是一个用 C# 编程语言写的简单代码,用来演示一个矩形的类和主程序的使用。首先定义了一个名为 Rectangle 的类,它有两个成员变量 length 和 width,分别表示矩形的长度和宽度。然后定义了三个方法,分别是 Acceptdetails、GetArea 和 Display,用来设置矩形的属性、计算矩形的面积和打印输出结果。接着定义了另一个名为 ExecuteRectangle 的类作为主程序,它首先创建一个 Rectangle 类的对象 r,然后调用它的 Acceptdetails 方法设置矩形的属性,再调用 Display 方法打印输出结果。最后等待用户输入,程序结束。
相关问题
在原来代码的基础上,为Cuboid类提供一个求长方体表面积的函数,使其函数名为calArea,并测试相应的功能。原来代码如下: #include <iostream> using namespace std; class Rectangle //矩形类 { private: int length; //矩形的长 protected: int width; //矩形的宽 public: Rectangle(int l = 10, int w = 10):length(l), width(w) { } int calArea() { return length * width; } }; class Cuboid : public Rectangle //长方体类,从Rectangle公有派生 { private: int height; //长方体的高 public: Cuboid(int l = 10, int w = 10, int h = 10): Rectangle(l, w), height(h) { } int calVolume() { return calArea() * height; } }; int main(void) { Cuboid test(10, 20, 30); cout << test.calVolume() << endl; return 0; }
可以在Cuboid类中添加一个求长方体表面积的函数calArea(),由于长方体有六个面,所以表面积为长方体的底面积加上四个侧面积的和,其中底面积为length和width的乘积,侧面积为长方体的高乘以底面的周长,即2*length+2*width。修改的代码如下:
```
#include <iostream>
using namespace std;
class Rectangle //矩形类
{
private:
int length; //矩形的长
protected:
int width; //矩形的宽
public:
Rectangle(int l = 10, int w = 10):length(l), width(w)
{
}
int calArea()
{
return length * width;
}
};
class Cuboid : public Rectangle //长方体类,从Rectangle公有派生
{
private:
int height; //长方体的高
public:
Cuboid(int l = 10, int w = 10, int h = 10): Rectangle(l, w), height(h)
{
}
int calVolume()
{
return calArea() * height;
}
int calArea()
{
return 2 * (length * width + width * height + length * height);
}
};
int main(void)
{
Cuboid test(10, 20, 30);
cout << "Cuboid volume: " << test.calVolume() << endl;
cout << "Cuboid surface area: " << test.calArea() << endl;
return 0;
}
```
测试输出如下:
```
Cuboid volume: 6000
Cuboid surface area: 1400
```
定义一个矩形类,有宽、高2个私有变量,有设置宽高、获取宽高、计算矩形的周长、计算矩形的面积4个共有方法。实例化对象并测试。 public class Rectangle { //声明width变量(double类型)、length变量(double类型); //声明带参方法void setLW(double l,double w),分别给length和width赋值 //声明无参方法void getLW(),输出width和length的值 //声明无参方法void zhouchang(),输出周长的计算结果 //声明无参方法void mianji(),输出面积的计算结果 } public class MainClass //另外单独写一个代码文件 { public static void main(String[] args) { //声明Rectangle对象r并用无参构造方法完成实例化 //调用setLW方法并传值 //调用方法r.zhouchang() //调用方法r.mianji() } }
以下是矩形类的实现代码:
```
public class Rectangle {
private double width;
private double height;
public void setLW(double l, double w) {
width = w;
height = l;
}
public void getLW() {
System.out.println("Width: " + width);
System.out.println("Height: " + height);
}
public void zhouchang() {
double perimeter = 2 * (width + height);
System.out.println("Perimeter: " + perimeter);
}
public void mianji() {
double area = width * height;
System.out.println("Area: " + area);
}
}
```
以下是测试代码:
```
public class MainClass {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.setLW(5.0, 3.0);
r.getLW();
r.zhouchang();
r.mianji();
}
}
```
输出结果如下:
```
Width: 3.0
Height: 5.0
Perimeter: 16.0
Area: 15.0
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)