简体   繁体   English

C#类成员访问

[英]C# class members access

C# class data members(fields or properties) can either be private or public or protected. C#类数据成员(字段或属性)可以是私有的,也可以是公共的或受保护的。

What if I want a private field for member methods use only and not to expose to the outside world? 如果我想让成员方法仅使用一个私有字段而不暴露给外界怎么办?

I can continue to use a private field, without breaking encapsulation or anything right? 我可以继续使用私有字段,而无需破坏封装或其他任何功能?

What I am not understanding is the two concepts: the data that we may need to expose to the outside world vs the data we may not need to do so (in the periphery of a class).. 我不理解的是两个概念:我们可能需要暴露给外界的数据与我们不需要这样做的数据(在类的外围)。

What are these two types of data while talking about building a class? 在谈论建立课程时,这两种数据是什么?

In the below example, the private field 'name' is private to the class but is still gettable/settable to/by external world. 在下面的示例中,私有字段“名称”是该类的私有字段,但仍可通过外部世界获取/设置。 Is the abstraction here then to 'not directly expose like 'here you go- have at it' but to add an indirect mechanism of access or update?? 那么,这里的抽象是不是“不像'这里就这么简单'那样直接公开”,而是要添加一种间接的访问或更新机制? Is that the encapsulation we are talking about here when we talk about public fields vs public properties? 当我们谈论公共领域与公共财产时,我们所说的是封装吗?

class Employee2
{
    private string name = "Harry Potter";
    private double salary = 100.0;

    public string GetName()
    {
        return name;
    }

    public void SetName(string title, string fullName)
    {
       this.name = title + fullName;        
    }

    public double Salary
    {
        get { return salary; }
    }
}

class PrivateTest
{
    static void Main()
    {
        Employee2 e = new Employee2();

        // The data members are inaccessible (private), so 
        // they can't be accessed like this: 
        //    string n = e.name; 
        //    double s = e.salary; 

        // 'name' is indirectly accessed via method: 
        string n = e.GetName();

        // 'salary' is indirectly accessed via property 
        double s = e.Salary;
    }
}

By using the protected keyword, you can limit a property or method to be accessible from within the class in which it is declared, and from within any class derived from the class that declared this member. 通过使用protected关键字,您可以限制一个属性或方法在声明它的类内以及从声明此成员的类派生的任何类内可访问。 (MSDN) (MSDN)

class Employee2
{
    private string name = "Harry Potter";
    private double salary = 100.0;

    protected string GetName()
    {
        return name;
    }

    protected string SetName(string newName)
    {
        this.name = newName;
    }

    protected double Salary
    {
        get { return salary; }

    }
}

If you are asking what's the point of having public get/set accessors when you could just make the actual field public, it's because then you have the freedom to change the class' private implementation of that field later, without changing client code. 如果您问何时可以将实际字段公开,那么使用公共获取/设置访问器有什么意义,这是因为这样您便可以在以后更改该字段的类的私有实现,而无需更改客户端代码。 For example, you could add a check if the setter is getting valid input, or even change the datatype of the private field entirely, but translate it correctly in the setter. 例如,您可以添加一个检查,以确定设置器是否获得了有效的输入,甚至可以完全更改私有字段的数据类型,但可以在设置器中正确转换它。

edit: 编辑:

SetName() as mentioned in commentaries, is an odd addition. 评论中提到的SetName()是一个奇怪的添加。 It should actually change the name (and take a parameter), not return a string. 它实际上应该更改名称(并带有参数),而不返回字符串。

encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition. 封装意味着对象的内部表示通常在对象定义之外的视图中不可见。 Typically, only the object's own methods can directly inspect or manipulate its fields 通常,只有对象自己的方法才能直接检查或操纵其字段

       private decimal accountBalance = 500.00m;

       public decimal CheckBalance() 
          {
            return accountBalance;
          }

and where get and set are useful for triggering events on change of values of object members 以及get和set对于触发对象成员的值更改事件很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM