当你用其他类构建一个类时,它叫什么?

时间:2012-12-27 01:36:46

标签: c# class

我最近在学习C#并了解基础知识。我不是在下面编写这段代码,而是试图理解它。在声明开始和结束字段时,线类使用Point。这在C#中被称为什么?

public class Point
{
    private float x;

    public float X
    {
        get { return x; }
        set { x = value; }
    }
    private float y;

    public float Y
    {
        get { return y; }
        set { y = value; }
    }

    public Point(float x, float y)
    {
        this.x = x;
        this.y = y;
    }

    public Point():this(0,0)
    {
    }

   }

}

class Line
{
    private Point start;

    public Point Start
    {
      get { return start; }
      set { start = value; }
    }

    private Point end;

    public Point End
    {
      get { return end; }
      set { end = value; }
    }

    public Line(Point start, Point end)
    {
        this.start = start;
        this.end = end;
    }

    public Line():this(new Point(), new Point())
    {
    }

1 个答案:

答案 0 :(得分:2)

我不确定你在问什么,但我想你想知道正确的术语:

public class Point // a class (obviously)
{
    private float x; // private field; also the backing
                     // field for the property `X`

    public float X // a public property
    {
        get { return x; }  // (public) getter of the property
        set { x = value; } // (public) setter of the property
                           // both are using the backing field
    }


    public float Y // an auto-implemented property (this one will
    { get; set; }  // create the backing field, the getter and the
                   // automatically, but hide it from accessing it directly)

    public Point(float x, float y) // constructor
    {
        this.x = x;
        this.Y = y;
    }

    public Point()  // a default constructor that calls another by
        : this(0,0) // using an "instance constructor initializer"
    {}
}