简体   繁体   中英

C# class members access

C# class data members(fields or properties) can either be private or public or protected.

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. (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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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