考虑以下项目:
创建一个新的Windows窗体应用程序项目并将其名称更改为MyRectangle . 创建一个名为Rectangle的类 . 添加名为_height的私有整数变量,以及另一个名为_width的私有整数变量 . 为两个变量添加访问器,将其称为“高度”和“宽度” . 让访问者读取和写入_height和_width变量 . 添加一个名为GetArea()的公共方法,它返回矩形的区域,另一个名为GetPerimeter(),返回矩形的周长 . 使用下面显示的代码来定义这些方法 .
public int GetArea()
{
return (_width * _height);
}
public int GetPerimeter()
{
return ((2 * _width) + (2 * _height));
}
修改公共MyRectangle()方法以创建Rectangle类的实例 . 使用高度访问器将矩形高度设置为6,使用宽度访问器将矩形宽度设置为8.调用GetArea()和GetPerimeter()方法,并将结果输出到控制台 .
---End of project instructions----
我创建了class.cs如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyRectangle
{
internal class Rectangle
{
// ** Properties **
private int _height = 0;
private int _width = 0;
// ** Accessors for Height**
public int Height
{
set
{
_height = value;
}
get
{
return _height;
}
}
// ** Accessors for Width **
public int Width
{
set
{
_width = value;
}
get
{
return _width;
}
}
public int GetArea()
{
return (_width * _height);
}
public int GetPerimeter()
{
return ((2 * _width) + (2 * _height));
}
}
}
我认为我的课程文件是正确的,但除此之外,我完全陷入困境 .
1 回答
您正确开发Rectangle类,继续重用它