【问题标题】:Make variable point to another variable使变量指向另一个变量
【发布时间】:2018-10-09 06:33:43
【问题描述】:

我需要一个实例,它是另一个实例的指针。基本上,我将从同一个类创建两个名为 AB 的实例。每当我更改 A 实例的属性时,B 实例的属性就会更改。基本上,属性将在内存中具有相同的地址。

我只想用不同的变量名访问同一个对象。每当其中一个被编辑时,另一个也应该被编辑。

如何在 Unity 中使用 c# 做到这一点?

【问题讨论】:

  • 你应该避免在 C# 中考虑指针
  • "我将更改 A 实例的属性,B 实例的属性将被更改。" - 这是否意味着您只有一个实例,但有两个变量(A 和 B)引用同一个实例?这就是 C# 对于引用类型的行为方式。我不确定你在问什么。你能澄清一下吗?
  • 例如:var a = new MyClass(); var b = a; a.Name = "John"; Console.WriteLine(b.Name); 将打印“John”,因为变量 ab 都指向 MyClass 的同一个实例。
  • 我将有 2 个相同类型的实例。每当其中一个被编辑时,另一个也应该被编辑。这更像是别名。
  • 你的意思是你只希望某些属性是通用的(就内存而言)?对同一个实例使用两个引用有什么好处吗?

标签: c# pointers unity3d


【解决方案1】:

我将有 2 个具有相同类型的实例。每当其中一个将是 编辑了,另外一个也应该编辑。

我只想用不同的变量名到达同一个对象。

您可以使用属性来伪造指向另一个变量。这可以通过getset 访问器轻松完成。

假设主变量名为 score:

public int score;

你可以用另外两个变量来指向 score 变量:

public int scoreWithDifferentName1
{
    get { return score; }
    set { score = value; }
}

public int scoreWithDifferentName2
{
    get { return score; }
    set { score = value; }
}

现在,您可以更改 score 变量或使用上面的这两个属性变量访问它:

scoreWithDifferentName1 = 0;
Debug.Log(scoreWithDifferentName1);

或者

scoreWithDifferentName2 = 3;
Debug.Log(scoreWithDifferentName2);

另一种选择是使用IntPtr,但这不是必需的。 C# 属性功能足以满足您的需求。这适用于值类型和引用类型。

【讨论】:

    【解决方案2】:

    这似乎是一个设计问题,即您希望类的外观以及它们的职责是什么。我不确定您所说的类的目的是什么,但这里明显的解决方案是带有 static 修饰符的属性。 向您的类添加静态属性将确保它在所有实例中具有相同的值,即:

    public class ClassX
    {
        public static string staticVar = "This is a static string";
        private string var1;
    }
    

    【讨论】:

      【解决方案3】:

      听起来您在描述引用类型在 C# 中的常规工作方式:

      public class MyClass
      {
          public string Name {get;set;}
      }
      
      void Test()
      {
          var a = new MyClass();
          a.Name = "Test";
          var b = a;
      
          Console.WriteLine(a.Name); // "Test"
          Console.WriteLine(b.Name); // "Test"
      
          b.Name = "MossTeMuerA";
          Console.WriteLine(a.Name); // "MossTeMuerA"
          Console.WriteLine(b.Name); // "MossTeMuerA"
      
          Mutate(a);
          Console.WriteLine(a.Name); // "John"
          Console.WriteLine(b.Name); // "John"
      }
      
      void Mutate(MyClass myClass)
      {
          myClass.Name = "John";
      }
      

      Example 1

      注意,如果要修改传递给方法的变量指向哪个类实例,需要使用ref关键字:

      void Test()
      {
          var a = new MyClass();
          a.Name = "Test";
          var b = a;
      
          Console.WriteLine(a.Name); // "Test"
          Console.WriteLine(b.Name); // "Test"
      
          Mutate(ref a);
          Console.WriteLine(a.Name); // "John"
          Console.WriteLine(b.Name); // "Test"
      }
      
      void Mutate(ref MyClass myClass)
      {
          myClass = new MyClass();
          myClass.Name = "John";
      }
      

      Example 2

      还有另一个关键字out,它允许方法通过传入要填充的变量来实例化调用者范围内的对象:

      void Test()
      {
          MyClass a;
          Instantiate(out a);
          Console.WriteLine(a.Name); // "John"
      }
      
      void Instantiate(out MyClass myClass)
      {
          myClass = new MyClass();
          myClass.Name = "John";
      }
      

      Example 3

      【讨论】:

        猜你喜欢
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 2013-12-21
        • 2015-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多