我怎样才能在C#中获取另一个变量的值
how can i get the value of another variable in C#
我正在尝试用 C# 统一编写一些代码,其中我有一个对象,我需要知道该对象的位置才能使我的脚本正常工作。我试图使用一个指针,因为我认为它是这样使用的。它说我必须使用不安全的标签,这让我觉得我做错了什么。我对此有点陌生,到目前为止,我对 C++ 的大部分了解都是我在 class 中学到的。我试着查找它,但找不到。这基本上就是我现在所拥有的。
using UnityEngine;
using System.Collections;
public class SGravSim : MonoBehaviour {
public GameObject moon;
public GameObject earth;
private struct Cords
{
public float* x
{
get
{
return x;
}
set
{
if (value != 0) <== this thing is realy just a placeholder
x = value;
}
}
public float* y
{
get
{
return y;
}
set
{
if (value != 0) <== this this is only in here for now
y = value;
}
}
public void DisplayX()
{
}
}
private Cords moonLocation;
private Cords earthLocation;
private Cords SataliteLocation;
// Use this for initialization
void Start () {
moonLocation.x = moon.transform.position.x;
moonLocation.y = moon.transform.position.y;
earthLocation.x = earth.transform.position.x;
earthLocation.y = earth.transform.position.y;
SataliteLocation.x = this.transform.position.x;
SataliteLocation.y = this.transform.position.y;
}
// Update is called once per frame
void Update () {
Debug.Log(moon.transform.position.x);
//
// Summary:
// The position of the transform in world space.
float yMoon = moon.transform.position.x
print(moonLocation.y);
}
}
我正计划制作这个集合,这样你就不能添加任何东西了。
我想每次我需要使用它时我都可以写出整个 earth.position.x 东西我只是想看看是否有更好的方法来做它以及我不能像我一样弄乱变量的方法想做的就是阅读它。
您可以使用:
private float x;
public float X
{
get
{
return x;
}
}
现在您只能在 class 中设置 x。
您可以在自动属性中使用私有集:
public float X {get; private set;}
这样只有您的 class 能够设置变量,而其他 class 则不能。
您收到不安全标签警告,因为您尝试使用实际上不安全的指针。可能有这样的用例,但在 C# 中,您通常使用 reference types and value types. In C# a struct is a value type, so it will behave differently compared to a reference type, as you can read here,这也是 Gubr 建议使用 class 而不是结构的原因。最后但同样重要的是,它们的存储方式有所不同,只是 google C# 堆和堆栈。
我还没有在 C# 中使用过那么多结构,所以我刚刚创建了一个新项目并进行了一些尝试。
所以我使用了你的代码,它也可以是这样的:
private struct Cords
{
public float x, y;
public void DisplayX(){}
}
正如其他人所提到的,您可以省略集合或将其设为私有并添加构造函数。请注意,私有集不等于不在自动属性中定义它。相反,它将创建一个只读字段。但是,在这两种情况下都必须调用 new 运算符来设置值:
private struct Cords
{
public float X { get; }
public float Y { get; }
public void DisplayX(){}
public Cords(float x, float y)
{
X = x;
Y = y;
}
}
在这里我们创建一个新的 Cords:
Cords earth = new Cords(10.005f, 12.689f);
你不应该在 c# 中使用指针,除非是非常特殊的情况。
这里几个人给你的解决方案很好:
public float X {get; private set;}
它被称为 属性 并且是 c# 中避免创建 getter 和 setter 的一种很好的可能性。
你说你对c++有一些了解,但c#其实更接近java等高级语言。您应该专注于面向对象的编码方式,忘记低级指针,尤其是使用 Unity 时。
我正在尝试用 C# 统一编写一些代码,其中我有一个对象,我需要知道该对象的位置才能使我的脚本正常工作。我试图使用一个指针,因为我认为它是这样使用的。它说我必须使用不安全的标签,这让我觉得我做错了什么。我对此有点陌生,到目前为止,我对 C++ 的大部分了解都是我在 class 中学到的。我试着查找它,但找不到。这基本上就是我现在所拥有的。
using UnityEngine;
using System.Collections;
public class SGravSim : MonoBehaviour {
public GameObject moon;
public GameObject earth;
private struct Cords
{
public float* x
{
get
{
return x;
}
set
{
if (value != 0) <== this thing is realy just a placeholder
x = value;
}
}
public float* y
{
get
{
return y;
}
set
{
if (value != 0) <== this this is only in here for now
y = value;
}
}
public void DisplayX()
{
}
}
private Cords moonLocation;
private Cords earthLocation;
private Cords SataliteLocation;
// Use this for initialization
void Start () {
moonLocation.x = moon.transform.position.x;
moonLocation.y = moon.transform.position.y;
earthLocation.x = earth.transform.position.x;
earthLocation.y = earth.transform.position.y;
SataliteLocation.x = this.transform.position.x;
SataliteLocation.y = this.transform.position.y;
}
// Update is called once per frame
void Update () {
Debug.Log(moon.transform.position.x);
//
// Summary:
// The position of the transform in world space.
float yMoon = moon.transform.position.x
print(moonLocation.y);
}
}
我正计划制作这个集合,这样你就不能添加任何东西了。 我想每次我需要使用它时我都可以写出整个 earth.position.x 东西我只是想看看是否有更好的方法来做它以及我不能像我一样弄乱变量的方法想做的就是阅读它。
您可以使用:
private float x;
public float X
{
get
{
return x;
}
}
现在您只能在 class 中设置 x。
您可以在自动属性中使用私有集:
public float X {get; private set;}
这样只有您的 class 能够设置变量,而其他 class 则不能。
您收到不安全标签警告,因为您尝试使用实际上不安全的指针。可能有这样的用例,但在 C# 中,您通常使用 reference types and value types. In C# a struct is a value type, so it will behave differently compared to a reference type, as you can read here,这也是 Gubr 建议使用 class 而不是结构的原因。最后但同样重要的是,它们的存储方式有所不同,只是 google C# 堆和堆栈。
我还没有在 C# 中使用过那么多结构,所以我刚刚创建了一个新项目并进行了一些尝试。
所以我使用了你的代码,它也可以是这样的:
private struct Cords
{
public float x, y;
public void DisplayX(){}
}
正如其他人所提到的,您可以省略集合或将其设为私有并添加构造函数。请注意,私有集不等于不在自动属性中定义它。相反,它将创建一个只读字段。但是,在这两种情况下都必须调用 new 运算符来设置值:
private struct Cords
{
public float X { get; }
public float Y { get; }
public void DisplayX(){}
public Cords(float x, float y)
{
X = x;
Y = y;
}
}
在这里我们创建一个新的 Cords:
Cords earth = new Cords(10.005f, 12.689f);
你不应该在 c# 中使用指针,除非是非常特殊的情况。 这里几个人给你的解决方案很好:
public float X {get; private set;}
它被称为 属性 并且是 c# 中避免创建 getter 和 setter 的一种很好的可能性。
你说你对c++有一些了解,但c#其实更接近java等高级语言。您应该专注于面向对象的编码方式,忘记低级指针,尤其是使用 Unity 时。