访问器问题

发布于 2024-09-07 22:31:27 字数 859 浏览 6 评论 0原文

我想知道是否不可能设置一个访问器来允许您访问访问器的变量。 错误示例:

    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 技术交流群。

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

发布评论

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

评论(6

感性 2024-09-14 22:31:27

我猜你没有发布完整的代码。我想现在您无法访问 Objec.name 因为它是私有变量(因此无法从 Main() 访问)。

尝试:

class Program
{
    public static void Main(string args[])
    {
        MyClass instance = new MyClass();
        instance.Child.ChildValue = "something";
    }
}

public class MyClass
{
    // The following code declares Public Properties 
    // rather than private variables.

    public string Value { get; set; }
    public string Name { get; set; }
    public MyChild Child { get; set; }

    public MyClass()
    {
        this.Child = new MyChild();
    }
}

public class MyChild
{
    public string ChildValue { get; set; }
}

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:

class Program
{
    public static void Main(string args[])
    {
        MyClass instance = new MyClass();
        instance.Child.ChildValue = "something";
    }
}

public class MyClass
{
    // The following code declares Public Properties 
    // rather than private variables.

    public string Value { get; set; }
    public string Name { get; set; }
    public MyChild Child { get; set; }

    public MyClass()
    {
        this.Child = new MyChild();
    }
}

public class MyChild
{
    public string ChildValue { get; set; }
}
千鲤 2024-09-14 22:31:27

好的,通过您的新示例,很清楚问题是什么:您的行为就好像 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.

泡沫很甜 2024-09-14 22:31:27

您必须为要在 C# 中公开的每个类成员指定 public

根据 C# 规范

类成员,包括嵌套的
类和结构,可以是公共的,
受保护的内部,受保护,
内部的,或者私人的。访问级别
对于类成员和结构成员,
包括嵌套类和结构,
默认情况下是私有的。

You have to specify the public for every class member you want to expose in C#.

According to the C# specification:

Class members, including nested
classes and structs, can be public,
protected internal, protected,
internal, or private. The access level
for class members and struct members,
including nested classes and structs,
is private by default.

我还不会笑 2024-09-14 22:31:27

您应该将名称和值变量公开,以便编译您的解决方案。或者更好的是,使用自动属性。

You should make the name and value variables public to get your solution to compile. Or better yet, use automatic properties.

雨落星ぅ辰 2024-09-14 22:31:27

您实际问题的答案“是否有办法执行上述操作(除了为每个值创建访问器之外)?”是否。

猜测一下您的意图,您似乎想要遵循一种名为 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/or Point). You will only expose to your class' audience the pieces of the original implementation that you want, in your question's case, every value.

我乃一代侩神 2024-09-14 22:31:27

您需要进行以下更改:

public void Main()
{
    Object.name = "test"; //Can access the object's properties now...
}

Objec ob = new Objec();

public Objec Object
{
    get { return ob; }
    set { ob = value; }
}

public class Objec
{
    public string name {get; set;}
    public string value {get; set;}
}

You need to make the following changes:

public void Main()
{
    Object.name = "test"; //Can access the object's properties now...
}

Objec ob = new Objec();

public Objec Object
{
    get { return ob; }
    set { ob = value; }
}

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