[英]How do I use get and set in C#?
我知道這是一個非常明顯的問題,但是根據我所看到的我還無法理解。
我不完全了解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。
很抱歉看到一個非常明顯的問題,但我似乎找不到答案。
您無法執行GetPos,因為pos是私有的,並且您沒有名為“ GetPos”的方法。 如果將pos公開,則可以使用Brick.pos進行獲取,並使用Brick.pos(position)進行設置。
這是編寫此類的方法:
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();
嘗試更改為此:
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()
};
當您使用C#聲明自動屬性時,它將在后台編譯為get和set方法,但是您甚至不必考慮這些。 您可以像訪問字段一樣訪問該屬性。
擁有該屬性的好處是,您可以輕松地將其替換為具有后備字段的更常規的屬性,以便可以在getter和/或setter中提供自定義邏輯。 但是,直到您需要它為止,這只是額外的噪音。 自動屬性提供了語法糖來避免這種噪音。
Brick brick = new Brick(1, Vector2.Zero, mySkin);
Vector2 oldPos = brick.pos;
brick.pos = new Vector2.One;
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);
但是,在后台,編譯器為每個屬性創建:
您應該這樣做:
Brick brick = new Brick(1, Vector2.Zero, mySkin);
Vector2 vec = brick.pos;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.