从另一个类访问和设置一个类的实例变量

本文关键字:一个 实例 变量 访问 另一个 设置 | 更新日期: 2024-10-18 07:21:35

我目前有一个名为:的类

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;
    }
}

而且,正如通过构造函数的params传递内容一样,在构建新实例时,只能使用/提供它们。这就是为什么它们被称为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。这意味着一旦创建了对象,就不能更改变量的值。任何人。周期

现在,你有几种方法可以做到这一点。首先是使用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的宽度和高度只能由HeatMap组件编辑,你可以让HeatMapComponent成为一个内部类,比如:

public class HeatMap
{
    private int width;
    private int height;
    public class HeatMapComponent
    {
        public HeatMap myHeatMap;
        public void SomeMethod()
        {
            myHeatMap.width = 10;
        }
    }
}

然而,我强烈建议你重新思考你想做什么。根据我的经验,公共内部类实际上相当罕见,因为它们很容易违反OOP原则。不同的应用程序设计可能更适合您。

两件事:

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可在同一程序集中访问。

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
    }
}

这听起来像是一道家庭作业题,问题是你没有理解课程。

以下是创建HeatMap类的方法。它包含一个重载,因此您可以在构造函数中设置宽度和高度,也可以通过set方法:

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类中使用它,您只需要创建HeatMap的实例。这里有两种方法:

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);
}

不过,一定要了解发生了什么。