访问器问题
我想知道是否不可能设置一个访问器来允许您访问访问器的变量。 错误示例:
public void Main()
{
Object.name = "test"; //Can't access the object's subproperties
}
Objec ob = new Objec();
public Objec Object
{
get { return ob; }
set { ob = value; }
}
class Objec
{
string name;
string value;
}
是否有办法执行上述操作(除了为每个值创建访问器之外)?
谢谢,
Max
编辑:这是一个更好的例子
public void Main()
{
//Now I can't change the X or Y properties, this will display an error
ThePoint.X = 10;
//To change the x value, I need to do the following:
ThePoint = new Point(10,0);
}
private Point Poi = new Point();
public Point ThePoint
{
get { return Poi; }
set { Poi = value; }
}
有没有办法让“ThePoint.X”工作(而不只是公开显示“Poi”)?
I was wondering if it was impossible to set up an accessor to allow you to access the accessor's variable..
Example of an error:
public void Main()
{
Object.name = "test"; //Can't access the object's subproperties
}
Objec ob = new Objec();
public Objec Object
{
get { return ob; }
set { ob = value; }
}
class Objec
{
string name;
string value;
}
Is there anyway to do the above (other than making accessors for every value)?
Thanks,
Max
EDIT: Here is a better example
public void Main()
{
//Now I can't change the X or Y properties, this will display an error
ThePoint.X = 10;
//To change the x value, I need to do the following:
ThePoint = new Point(10,0);
}
private Point Poi = new Point();
public Point ThePoint
{
get { return Poi; }
set { Poi = value; }
}
Is there a way to make 'ThePoint.X' work (without just publicly displaying 'Poi')?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我猜你没有发布完整的代码。我想现在您无法访问 Objec.name 因为它是私有变量(因此无法从 Main() 访问)。
尝试:
I'm guessing you didn't post full code. I would imagine right now you can't access Objec.name because it is a private variable (and thus inaccessible from Main()).
Try:
好的,通过您的新示例,很清楚问题是什么:您的行为就好像 Point 是一个类,而实际上它是一个结构体。
当您说
ThePoint.X = 10;
时,您所做的就是更改原始点副本的 X。这是因为结构总是按值传递,而不是引用。简而言之,错误在于你的期望。
Ok, with your new example, it's clear what the problem is: you're acting as if Point were a class when it's actually a struct.
When you say
ThePoint.X = 10;
, all you're doing is changing the X for a copy of the original point. That's because a struct is always passed by value, not reference.In short, the error is in your expectations.
您必须为要在 C# 中公开的每个类成员指定
public
。根据 C# 规范:
You have to specify the
public
for every class member you want to expose in C#.According to the C# specification:
您应该将名称和值变量公开,以便编译您的解决方案。或者更好的是,使用自动属性。
You should make the name and value variables public to get your solution to compile. Or better yet, use automatic properties.
您实际问题的答案“是否有办法执行上述操作(除了为每个值创建访问器之外)?”是否。
猜测一下您的意图,您似乎想要遵循一种名为 Bridge 的设计模式(您的类是围绕
Object
和/或Point
实现的抽象。您只会向班级观众展示您想要的原始实现的各个部分,在您的问题的情况下,每个值。The answer to your actual question "Is there anyway to do the above (other than making accessors for every value)?" is NO.
Guessing a bit on your intentions, you seem to want to follow a design pattern known as Bridge (your class is an abstraction around the implementation of
Object
and/orPoint
). You will only expose to your class' audience the pieces of the original implementation that you want, in your question's case, every value.您需要进行以下更改:
You need to make the following changes: