您的位置:首页 > 编程语言 > C#

c# 字段,属性

2010-04-27 17:27 330 查看
public class Rectangle
{
private double _width;
private double _length;

public double Width
{
//set { _width = value; }
//get { return _width; }
set
{
if (value == 3)
Console.WriteLine("set _width = 3");
_width = value;
}
get
{
if (_width == 3)
Console.WriteLine("get _width = 3");
return _width;
}
}

public double Length
{
set { _length = value; }
get { return _length; }
}

public Rectangle(double width, double length)
{
//_width = width;
_length = length;
Width = width;
//Length = length;
}

public double Perimeter()
{
return 2 * (Width + Length);
//return 2 * (_width + _length);
}
public double Area()
{
//return Width * Length;
return _width * _length;
}
}

class Program
{
static void Main(string[] args)
{
double S,C;
Rectangle rec=new Rectangle(3,4);
S=rec.Area();
C=rec.Perimeter();
Console.WriteLine("面积为:{0},周长为:{1}", S, C);
Console.ReadLine();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: