This is silly.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.