In the past day, I found that I had always had a serious misunderstanding about class encapsulation. There is such a code in ISEP's C:
Class person
...{
Public Person (string name)
...{
This. Name = Name;
}
Public void sayhello (person another)
...{
Console. writeline ("hello," + another. Name + ". I 'm" + name );
}
Private string name;
}
When I saw the sayhello () method, I found it very problematic. How can I call the private field name of a person instance another. I have always thought that private modification cannot be used by the outside world. In fact, this is not very strict. The outside world should refer to other classes or other types of objects, while different instances of the same class can access each other's private members. For example, you can write in the main function as follows:
Static void main (string [] ARGs)
...{
Person p1 = new person ("Kimi ");
Person P2 = new person ("massa ");
P1.sayhello (P2 );
Console. readkey ();
}
The console outputs hello, massa. I'm Kimi.
I tried it again with C ++. The same is true.