Compare the access control Syntax of Java and C ++

Source: Internet
Author: User

Continue with the previous article. In fact, writing this kind of thing is quite boring. c ++ generally doesn't look at this thing, and Java thinks it is too shallow. In addition, this article is suspected of instigating C ++ programmers to learn Java or Java programmers to learn C ++, which is easily disgusted by the two strong supporters. There is no way. I always thought that the best way to understand a person is to think about the problem from the perspective of others. With regard to the comparison between Java and C ++, those Java designers should have gone through some careful consideration, so let me continue to think about these unique ways of thinking.

Access Control in C ++ is simple and clear. The main syntax points are as follows:

  1. There are three types of access control: public, protected, and private.
  2. Access control can modify member variables, member functions, and base classes.
  3. Public (public), protected (public only to sub-classes), private (private)
  4. Access control is free. in various situations, such as internal classes and local classes, as long as the member variables are modified, the member functions and the base classes are randomly written, and the semantics remains unchanged.
  5. Access control can be damaged to a limited extent, which is the role of the friend keyword. However, such damage is strictly restricted and must be applied by the damaged class. Otherwise, no access is allowed.

Access Control in Java is a bit messy. The main syntax points are as follows:

  1. There are four types of access control: public, protected, default, and private.
  2. Access control can modify member variables, member functions, and classes (note that permissions of a single class cannot be modified in C ++, but permissions of the base class cannot be modified in Java)
  3. The access control semantics are public, protected (public to the same package and subclass), default (public to the same package), private (private), and what is a package? In short, it is the class defined in all files in the same directory. That is to say, in a directory, all the protected and default permissions are useless paper. For example, if you want to make public to the same package, it is equivalent that as long as everyone lives in the same community, everything except yourself is private. What? Your wedding photo with your wife? What are the tuition fees you leave to your son? Don't talk nonsense, all public. It is like entering a communist society.
  4. Top-level classes cannot be modified using protected or private. The so-called top-level class is a class without a base class (strictly speaking, there are also Object base classes). This rule is also somewhat overbearing, by default, protected is not allowed.
  5. The external class can access everything in the internal class, even if the private class in the internal class does not work, it is like saying: "Little girl, now in my hands, you just got it." Unless the internal class itself is declared as private, there is no such girl, but it also goes from one extreme to another, the external class cannot access anything of the internal class at all, even if the internal class is public.
  6. The internal class can access everything in the external class, even if the external class is private, it is really not a gentleman. Now, the internal class is turning over and taking the lead, how about a whole? Day and night defense, it's hard to defend against one piece of money."
  7. External methods can access all the things of the local class, even if the private in the local class does not work, it is also a small girl who falls into the hands of thieves, and this time the chance to become a pure girl is gone, public, protected, and private modification are not allowed for local classes. It can only be regarded as the default value.
  8. A local class can access everything in an external class, even if it is private in an external class, it is also an example of a piece of cake that is hard to prevent, but this thief is too much, I thought that the local class can only be closed in the cage of the method, and the class to which the method belongs cannot be spared.

Sample Code for Rule 8:

Class D
{
Private int;

Public void test ()
{
Class inner
{
Public int m;
Private int N;
}

Inner in = new inner ();
In. M = 10;
In. n = 20;
A = 100;

TTT ();
}

Private void TTT ()
{
}
}

To sum up:

The access control of C ++ is simple and clear. Of course, it should also acknowledge that access control may be damaged, so the right to choose is handed over to the programmer and the friend keyword is introduced.

Java's access control is complex and fuzzy. Of course, it also needs to solve the problem that access control is damaged, but it may think that the method of friend is too violent, it is still necessary to take advantage of the compiler's own capabilities to increase unnecessary control and reduce the possibility of code errors. But let's take a look at this pile of rules. Don't worry about whether the compiler is complaining. At least the programmer can understand it, and do we have some choices? Again, it proves that Java's trust in programmers is far less than C ++'s trust in programmers. These two world views are different.

Related Article

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.