getter和setter的类C#类

getter和setter的类C#类

本文介绍了getter和setter的类C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如

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
}

我将如何实施OuterClass?

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

在此先感谢!

推荐答案

的语法也不会有什么不同。只是......

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

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



顺便说一句,如果你使用C#.NET 3.5中,您可以使用自动属性生成功能,如果你已经是一个简单的属性,只是读取和写入后备存储(如您有上面)。该语法来类似于一个抽象的属性:

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:

public InnerClass InnerClass { get; set; }

这会自动生成用于存储的私有成员,然后在获得,并在设置

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

这篇关于getter和setter的类C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:04