题目要求 定义一个circle类,它包含两个属性,即圆心属性和半径属性:两个方法,即计算圆周长和圆面积。同时设计测试类,以测试circle类的有效性! 
代码如下:我不明白的是我自己把属性定义的那部分去掉以后,结果和不去掉的一样 ,那为什么还要定义属性呢?
这样有什么好处?
还有定义属性的时候为什么要定义 _centerx, _centery, _r=0呢?
只定义 centerx, centery, r=0不行吗?using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
circle cir = new circle();
cir.centerx = 1;
cir.centery = 2;
Console.WriteLine("请输入圆的半径:");
cir.r = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("圆的周长为:"+ Convert.ToString( cir.girth()));
Console.WriteLine("圆的面积为:" + Convert.ToString(cir.area()));
Console.ReadLine();
}
}
class circle
{
private double _centerx, _centery, _r=0;/// <summary>
/// 圆心的x坐标属性
/// </summary>
public double centerx
{
get { return _centerx; }
set { _centerx = value; }
}
/// <summary>
/// 圆心的y坐标属性
/// </summary>
public double centery
{
get { return _centery; }
set { _centery = value; }
}
/// <summary>
/// 园的半径
/// </summary>
public double r
{
get { return _r; }
set { _r = value; }
}
/// <summary>
/// 计算周长的方法
/// </summary>
/// <returns></returns>
public double girth()
{
return 2 * Math.PI * _r;
}
/// <summary>
/// 计算面积的方法
/// </summary>
/// <returns></returns>
public double area()
{
return Math.PI * _r * _r;
}
}
}

解决方案 »

  1.   

    属性可以有get;set来控制属性的赋值,但是字段不行属性一般情况下都会操作一个字段,就像你的程序中一样。属性r操作字段_r
      

  2.   

    属性的目的就是对类中的敏感字段的赋值进行约束。
    比如我有一个Student类,类中有一个Age(姓名)整型字段public class Student
    {
       public student(){}
       
       public int Age;   public void say()
      {
         Console.Write("年龄是{0}",this.Age.ToString());
      }
    }编写一个测试类,如果我实例化了一个Student类对象,将年龄字段赋值为了1000,程序也会正常编译并且运行。
    但是年龄为1000,这个情况在我们现实生活中是不允许的。
    所以,.NET就提供了属性,来控制对敏感字段的赋值。实现属性的步骤:
    一、将敏感字段私有化
    二、提供属性,并且提供一组get和set访问器
    public class Student
    {
       public Student(){}   private int age;   //将敏感字段私有化    //提供属性
        public int Age
       {
          get{return age;}   //get访问器
            set
          {
              //在set访问器中进行逻辑判断,value为set访问器的属性,它的值就是Age的值。
              if(value > 0 && value < 100)
              {
                 age = value;
              }
              else
              {
                  age = 20;
              }
          }
       }    public void say()
       {
           Console.Write("年龄是{0}",this.Age.ToString());
       }
    }
    测试以上代码,因为age字段已经私有化了,所以要对age字段进行访问,必须通过Age属性,
    其下的set访问器对用户的赋值进行判断,就相当于一个过滤器,对敏感字段的数据安全性做保障。
    get访问器的主要作用就是读取。那么,测试上一段代码,当我们输入了小于0 或者 大于100的数值给与Age时,
    输出的结果会是20.那么,以上的属性是C#的老写法。
    在C#3.0中,提供了一个自动属性的新特性。
    对于不必进行逻辑判断的属性,我们可以这样写了。public int Age{get;set;}这种写法比老写法节省了2/3的代码量,但是少去的代码并没有真正的减少。
    它会在由C#编译器编译为MSIL(微软中间代码)的时候会自动添加上,可以利用反射工具进行查看。
      

  3.   

    有很大的区别,比如数据范围的限制如果只定义字段 double x, y, r;那么,我对 r 输入一个负数,你的程序就会出错,甚至崩溃可使用属性定义,就可以防止这种情况的发生// 私有字段
    double _radius = 0;// 公开属性
    public double Radius {
        get { return _radius; }
        set { if(value < 0) value = 0; _radius = value; }
    }
    还可以扩展代码,从而产生事件public double Radius {
        get {
            return _radius;
        }
        set {        if(value < 0) value = 0;        if(_radius != value) {
                _radius = value; this.OnRadiusChanged(new EventArgs());
            }
        }
    }// 事件定义
    public event EventHandler RadiusChanged;// 事件处理 受到保护
    protected void OnRadiusChanged(EventArgs e) {
        if(this.RadiusChanged != null) {
            this.RadiusChanged(this, e);
        }
    }