如何在C#中使用get和set?

时间:2014-03-25 16:25:18

标签: c# visual-studio-2008

我知道这是一个非常明显的问题但是从我所看到的我还不能得到它。

我并不完全理解Getters和Setters如何在C#中工作,例如,我有一个游戏角色的代码我应该为大学做的:

namespace Clase_25_3_2014
{
    class Brick
    {
        private int speed { get; set; }
        private Vector2 pos { get; set; }
        private Texture2D skin { get; set; }

        public Brick(int speed, Vector2 position, Texture2D skin)
        {
            this.Speed(speed);
            this.Pos(position);
            this.Skin(skin);
        }

    }
}

现在,就目前而言,在我使用该类的地方,我试着像这样称呼它:

Brick brick = new Brick(1, Vector2.Zero, mySkin);
brick.GetPos();

现在,显然这对你们来说很奇怪,那是因为我还没有发现我应该如何正确使用它,以便它像brick.getPos();来自java。

对于这个非常明显的问题感到抱歉,但我似乎无法找到答案。

5 个答案:

答案 0 :(得分:4)

你不能做GetPos,因为pos是私有的,你没有名为“GetPos”的方法。如果你公开pos,你可以使用Brick.pos来获取和设置Brick.pos(位置)。

以下是您可以编写此课程的方法:

namespace Clase_25_3_2014
{
    class Brick
    {
        public Brick(int speed, Vector2 position, Texture2D skin)
        {
            this.Speed = speed;
            this.Pos = position;
            this.Skin = skin;
        }

        public int Speed { get; set; }
        public Vector2 Pos { get; set; }
        public Texture2D Skin { get; set; }
    }
}

班级访问的类型:

// lots of explicity (is that a word? :)
public MyClass
{
  // Field
  // It is recommended to never expose these as public
  private int _myField; 

  // Property (old school, non-auto private field)
  public int MyProperty
  {
    public get
    {
      return this._myField;
    }
    public set
    {
      this._myField = value;
    }
  }

  // Property (new school, auto private field)
  // (auto field cannot be accessed in any way)
  public int MyProperty2 { public get; private set; }

  // Method (these are not needed to get/set values for fields/properties.
  public int GetMyMethod()
  {
    return this._myField;
  }
}


var myClass = new MyClass;

// this will not compile,
// access modifier says private
// Set Field value
myClass._myField = 1;  

// Get Property Value
var a = myClass.MyProperty;

// Set Property Value
myClass.MyProperty = 2;

// Get Property Value
var b = myClass.MyProperty2;

// this will not compile
// access modifier says private
// Set Property Value
myClass.MyProperty2 = 3;

// Call method that returns value
var c = myClass.GetMyMethod();

答案 1 :(得分:2)

尝试更改为:

    public Brick(int speed, Vector2 position, Texture2D skin)
    {
        this.Speed = speed;
        this.Pos = position;
        this.Skin = skin;
    }

使用C#,你不需要这种类型的构造函数。您可以使用以下方式声明此类的对象而不使用构造函数:

public Brick brickTest(){
   Speed = 10,
   Position = new Vector2(),
   Skin = new Texture2D()
};

答案 2 :(得分:2)

当您在C#中声明一个自动属性时,它将在幕后编译为get和set方法,但您甚至不需要考虑这些。您可以像访问字段一样访问该属性。

拥有该属性的好处是,您可以轻松地将其替换为具有支持字段的更常规属性,以便您可以在getter和/或setter中提供自定义逻辑。然而,在您需要它之前,它只是额外的噪音。自动属性提供了语法糖以避免噪音。

Brick brick = new Brick(1, Vector2.Zero, mySkin);
Vector2 oldPos = brick.pos;
brick.pos = new Vector2.One;

答案 3 :(得分:1)

namespace Clase_25_3_2014
{
    class Brick
    {
        public int Speed { get; set; }
        public Vector2 Pos { get; set; }
        public Texture2D Skin { get; set; }

        public Brick(int speed, Vector2 position, Texture2D skin)
        {
            this.Speed = speed;
            this.Pos = position;
            this.Skin = skin;
        }

    }
}

在外面使用:

Brick brick = new Brick(1, Vector2.Zero, mySkin);
Console.WriteLine(Brick.Pos);

但是,编译器为每个Property创建的幕后花絮:

  • Property-type的私有变量存储。
  • 两个功能(Get \ Set)。
  • 将用于其功能的属性翻译。

答案 4 :(得分:0)

你应该这样做:

Brick brick = new Brick(1, Vector2.Zero, mySkin);
Vector2 vec = brick.pos;