簡體   English   中英

從另一個類訪問和設置一個類的實例變量

[英]Accessing and setting the instance variable of one class from another class

我目前有一個名為:

public class HeatmapComponent : GH_Component

我還有另一門課:

public class HeatMap

Heatmap類中,我有兩個實例變量聲明為:

public int _width;
public int _height;

我希望能夠從HeatmapComponent類訪問並設置_width_height變量。 我知道這是一個范圍問題,但是對於需要做什么我有些困惑。

在我的HeatmapComponent類中,這是我想到的:

this._width = width; // width is declared somewhere in this class
this._height = height; // height is same as above

如果這是一個愚蠢的問題,我事先表示歉意。 如果我缺少代碼段,請告訴我。 我很樂意提供。

您要設置這兩個字段的值嗎? 它們是readonly 只能在構造函數中執行此操作。

public class HeatMap
{
    private readonly int _width;
    private readonly int _height;

    public HeatMap(int wid, int hei)
    {
        _width = wid;
        _height = hei;
    }
}

並且,就像通過構造函數的參數傳遞內容一樣,您只能在構建新實例時使用/提供它們。 這就是為什么它們被稱為constructorreadonly fields

public class HeatmapComponent
{
    private int widthOfMap;
    private int heightOfMap;

    void createMapAndDoSomething() 
    {
        var hmap = new HeatMap(widthOfMap, heightOfMap);
        hmap.thing();
    }
}

如果您不想創建新的HeatMap,並且希望在任何時間從某個“外部”位置設置寬度/高度,則:

  • 它們不能是只讀的
  • 必須存在一些公開的更改方式

例如:

public class HeatMap
{
    private int _width;
    private int _height;

    public void SetSize(int wid, int hei)
    {
        _width = wid;
        _height = hei;
    }
}

public class HeatmapComponent
{
    private int widthOfMap;
    private int heightOfMap;

    private HeatMap oldMap;

    void changeTheSizes() 
    {
        oldMap.SetSize(widthOfMap, heightOfMap);
    }
}

有時甚至更好,請使用屬性:

public class HeatMap
{
    private int _width;
    private int _height;

    public int Width { set { _width = value; } }
    public int Height { set { _height = value; } }
}

public class HeatmapComponent
{
    private int widthOfMap;
    private int heightOfMap;

    private HeatMap oldMap;

    void changeTheSizes() 
    {
        oldMap.Width = widthOfMap;
        oldMap.Height = heightOfMap;
    }
}

幾件事情:

readonly關鍵字使任何內容只能在constructor 例:

class XYZ
{
    private readonly int x;
    public XYZ()
    {
        x = 10; //works
    }

    public void SomeMethod()
    {
        x = 100; //does not work since it is readonly
    }
}

然后是各種訪問修飾符: private只能在類本身中訪問, protected可以在繼承的類中訪問, public可以在任何地方訪問。 Internal可以在同一程序Internal訪問。

public class HeatMapComponent
{
    HeatMap _map;
    public HeatMapComponent()
    {
        _map = new HeatMap();
    }
    public void SomeMethod()
    {
        _map.Width = 10; //should work if Width is public and not readonly and if _map was initialized already, ie not null
    }
}

在我回答您的問題之前,您有一個主要的主要問題: readonly 這意味着一旦創建對象,就無法更改變量的值 被任何人。 期間

現在,您有幾種方法可以做到這一點。 首先是使用Snorre所說的屬性。 實際上,您將獲得以下信息:

public class HeatMap
{
   public int Width { get; set; }
   public int Height { get; set; }
}

public class HeatMapComponent
{
    private HeatMap myHeatMap; // Must have a reference to the object you want to change!

    public void SomeMethod()
    {
        myHeatMap.Width = 10;
    }
}

現在,這里明顯的缺點是任何人都可以更改HeatMap的屬性。 如果出於某種原因,您確實真的想使HeatMap的寬度和高度只能由HeatMapComponent編輯,則可以使HeatMapComponent成為內部類 ,如下所示:

public class HeatMap
{
    private int width;
    private int height;

    public class HeatMapComponent
    {
        public HeatMap myHeatMap;

        public void SomeMethod()
        {
            myHeatMap.width = 10;
        }
    }
}

但是,我強烈建議您重新考慮您要做什么。 根據我的經驗,公共內部類實際上很少見,因為它們很容易違反OOP原則。 不同的應用程序設計可能更適合您。

這聽起來像是一個作業問題,但問題是您對課程的理解不夠。

這是創建HeatMap類的方法。 它包含一個重載,因此您可以在構造函數中或通過Set方法設置Width和Height:

public class HeatMap {

  public HeatMap() {
    Width = 0;
    Height = 0;
  }

  public HeatMap(int width, int height) {
    Width = width;
    Height = height;
  }

  public void Set(int width, int height) {
    Width = width;
    Height = height;
  }

  public int Width { get; private set; }

  public int Height { get; private set; }

}

要在HeatmapComponent類使用它,你只需要創建熱圖的一個實例。 這有兩種方法:

public HeatmapComponent() {
}

public void Test1(int width, int height) {
  var hm = new HeatMap(width, height);
  Console.WriteLine("Width: {0}, Height: {1}", hm.Width, hm.Height);
}

public static void Test2(int width, int height) {
  var hm = new HeatMap();
  hm.Set(width, height);
  Console.WriteLine("Width: {0}, Height: {1}", hm.Width, hm.Height);
}

不過,請確保您了解發生了什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM