c#类中类的getter和setter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/847905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:00:58  来源:igfitidea点击:

getter and setter for class in class c#

c#classmembersettergetter

提问by

Assuming we have a class InnerClass with attributes and getter/setter. We also have a class OuterClass containing the InnerClass.

假设我们有一个带有属性和 getter/setter 的类 InnerClass。我们还有一个包含 InnerClass 的类 OuterClass。

e.g.

例如

class InnerClass
{
    private int m_a;
    private int m_b;

    public int M_A
    {
        get
        {
             return m_a;
        }
        set
        {
             m_a = value;
        }
     }
}

class OuterClass
{
    private InnerClass innerClass
}

How would I implement a correct getter and setter for the innerClass member of OuterClass?

我将如何为 OuterClass 的 innerClass 成员实现正确的 getter 和 setter?

Thanks in advance!

提前致谢!

回答by Adam Robinson

The syntax wouldn't be any different. Just...

语法不会有任何不同。只是...

public InnerClass InnerClass
{
    get { return innerClass; }
    set { innerClass = value; }
}

By the way, if you're using C# in .NET 3.5, you can use the automatic property generation feature if all you have is a simple property that just reads and writes to a backing store (like you have above). The sytax is similar to that of an abstract property:

顺便说一下,如果您在 .NET 3.5 中使用 C#,并且您拥有的只是一个简单的属性,它只是读取和写入后备存储(就像上面那样),那么您可以使用自动属性生成功能。语法类似于抽象属性的语法:

public InnerClass InnerClass { get; set; }

This automatically generates a private member for storage, then reads from it in the getand writes to it in the set.

这会自动生成一个用于存储的私有成员,然后getset.

回答by edosoft

public InnerClass InnerClass
    {
        get
        {
             return innerClass;
        }
        set
        {
             innerClass = value;
        }
     }

回答by James

It depends on how the inner class should work. The outer class might need to "own" the inner class, in which case:

这取决于内部类应该如何工作。外部类可能需要“拥有”内部类,在这种情况下:

public InnerClass InnerClass{
  get{ return innerClass; }
  set{ innerClass.CopyFrom(value); /* Pseudo method call */ }
}

By doing it this way, you prevent outside code from manipulating the instance unless explicitly through OuterClass..

通过这样做,您可以防止外部代码操作实例,除非明确通过 OuterClass..

回答by Daniel Brückner

If you mean accessing inner class members without exposing the inner class itself, you can use the following code. If you just want to expose this.innerClass, there is no difference to the way you expose the fields of InnerClass.

如果您的意思是访问内部类成员而不暴露内部类本身,则可以使用以下代码。如果您只是想公开this.innerClass,则公开 的字段的方式没有区别InnerClass

class OuterClass
{
    private InnerClass innerClass

    public int M_A
    {
        get
        {
            if (this.innerClass != null)
            {
                 return this.innerClass.M_A;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        set
        {
            if (this.innerClass != null)
            {
                 this.innerClass.M_A = value;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
    }
}

回答by BenAlabaster

The most elegant way of doing this is to use implicit getters and setters:

最优雅的方法是使用隐式 getter 和 setter:

class InnerClass
{
  public int a{ get; set; }
  public int b{ get; set; }
}

class OuterClass
{
  public InnerClass innerClass{ get; set; }
}

This is implicitly the same as:

这隐含地等同于:

class InnerClass
{
    private int _a;
    public int a
    {
        get
        {
            return _a;
        }
        set
        {
            _a = value;
        }
    }

    private int _b;
    public int b
    {
        get
        {
            return _b;
        }
        set
        {
            _b = value;
        }
    }
}

class OuterClass
{
    private InnerClass _innerClass;
    public InnerClass innerClass
    {
        get
        {
            return _innerClass;
        }
        set
        {
            _innerClass = value;
        }
    }
}

These two definitions are implicitly the same - minus quite a few keystrokes. In the first example, the compiler will implement the necessary private fields behind the scenes so you don't have to. The second, however gives you more control of what is going on.

这两个定义隐含相同 - 减去相当多的按键。在第一个示例中,编译器将在幕后实现必要的私有字段,因此您不必这样做。但是,第二个使您可以更好地控制正在发生的事情。