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();
}
}
{
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();
}
}
相关文章推荐
- C#中属性和字段的区别
- C#中的字段与属性
- C#中属性和字段的区别
- C# 字段与属性区别
- c#学习摘录:类\继承\接口\字段、方法、属性\浅深复制(4)
- C# 属性与普通字段变量的区别
- (转)C#.NET如何不序列化字段、属性
- C#属性和字段的区别
- C#中属性和字段的区别
- C# 属性和字段的区别
- C#中的类:字段、属性、方法
- C# 字段与属性
- C#属性和字段的认识
- C#属性、字段和成员变量的关系区别
- 【C#】-属性(Property)和字段(Field)的区别
- c# 字段,属性和方法总结
- C#中字段、属性、只读、构造函数赋值、反射赋值的问题
- C# 类型和成员基础以及常量、字段、属性
- C#中属性与字段的用法
- C#中属性和字段的区别