C# Getter/Setter 问题

发布于 2024-08-18 16:45:33 字数 318 浏览 12 评论 0原文

假设我在一个类中有一个属性:

Vector3 position{get; set;}

所以我在某个地方创建了该类的一个实例,现在我想更改position.x,现在这是不可能的,因为 getter 和 setter 设置并获取整个对象。所以我必须让一个临时 Vector3 改变它的值,然后分配它。

通常我会将位置设置为公共领域,以便解决问题。但在这种情况下我不能这样做,因为位置是接口的实现,而接口不能有字段。

那么我怎样才能最好地解决这个问题呢?

编辑: Vector3 是一个结构体,因此它是一个值类型

Say i have a property in a class:

Vector3 position{get; set;}

So i create an instance of that class somewhere and now i want to change position.x, that would be impossible now because the getter and setter set and get the whole object. So i have to make a temporary Vector3 change its values and then assign it.

Normally i would make position a public field so that problem would be solved. But i cant do it in this case because position is an implementation of an interface and interfaces cant have fields.

So how can i solve this the best way.

Edit: Vector3 is a struct so its a value type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹微笑 2024-08-25 16:45:33

直接的解决方案是否不可接受?

foo.position = new Vector(newX, foo.position.Y, foo.position.Z);

这有什么问题吗?这看起来非常简单。

Is the straightforward solution somehow not acceptable?

foo.position = new Vector(newX, foo.position.Y, foo.position.Z);

What's wrong with that? It seems perfectly straightforward.

丶情人眼里出诗心の 2024-08-25 16:45:33

IMO,最简单的答案在这里:

private Vector3 position;
public Vector3 Position {
    get {return position;}
    set {position = value;} // with @Mehrdad's optimisation
}
public float X {
    get {return position.X;}
    set {position.X = value;}
}
public float Y {
    get {return position.Y;}
    set {position.Y = value;}
}
public float Z {
    get {return position.Z;}
    set {position.Z = value;}
}

现在,如果您只需要更改一个,则可以更改 obj.Xobj.Yobj.Z尺寸,或更改 obj.Position 来更改所有内容。

如果您需要名称 position 来实现接口,请显式执行:

Vector3 IWhateverInterface.position {
    get {return position;}
    set {position = value;}
}

IMO, the easiest answer here:

private Vector3 position;
public Vector3 Position {
    get {return position;}
    set {position = value;} // with @Mehrdad's optimisation
}
public float X {
    get {return position.X;}
    set {position.X = value;}
}
public float Y {
    get {return position.Y;}
    set {position.Y = value;}
}
public float Z {
    get {return position.Z;}
    set {position.Z = value;}
}

Now you can change obj.X, obj.Y and obj.Z if you only need to change one dimension, or change obj.Position to change everything.

If you need the name position to implement an interface, then do it explicitly:

Vector3 IWhateverInterface.position {
    get {return position;}
    set {position = value;}
}
洛阳烟雨空心柳 2024-08-25 16:45:33

这是可变值类型的问题之一。您可以使用新的 X 值创建该值类型的新实例,然后重新分配给该属性。您可以通过提供有用的构造函数或添加返回修改对象(而不是修改值)的方法来简化实例创建。

That's one of the issues with mutable value types. You can create a new instance of the value type with the new X value and reassign in to the property. You can make the instance creation easier by providing useful constructors or adding methods that return a modified object (instead of modifying the value).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文