The access control mechanism for C/C + + Java than teaching------

Source: Internet
Author: User
Tags control characters

1. Java
The keywords in Java that represent permissions are public,protected,private. No friendly this, nothing to write is also a different permission representation.

 class Mall{     PublicString Mallname;} class supermarket extends Mall{     Public voidShow () {System.out.println ("This is A supermaket:");    System.out.println (Mallname); }} Public  class HelloWorld{     Public Static voidMain (String args[]) {Supermarket supermarket =NewSupermarket (); Supermarket.mallname ="Gao";    Supermarket.show (); }}

In simple terms:
Members of public can be accessed by this class, this package, subclasses, subclasses, external packages
Members of the protected can be accessed by this class, this package, subclasses, but not by external packages
The member of default can be accessed by this class, this package
Private can only be accessed by this class

Java files can be made up of a number of top-level classes, where there are only two possible locations, either in all locations (public) or in this package (default). However, a file can have only one external class with the same name that has a public adornment. Other external classes are decorated by default.

The outer class can contain inner classes, inner classes can be decorated in four ways, the description of internal classes under the Java topic.

3. C #
The modifiers for the access control mechanism in C # are public,private, protected,internal
Of these, only protected and internal can be decorated. Whether it is a class or a member of a class, if you do not write the adornment defaults think internal

As you can see, private can only be accessed within the class, protected can only be accessed in this class and subclass, internal can be accessed from within this assembly, and public can be accessed anywhere. Protected internal is the meaning of protected or internal.

For a class, public means that it can be accessed anywhere, internal means that it can only be accessed within the same assembly. The so-called same assembly refers to a program under different project, not a different namespace.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Runtime.InteropServices; namespace ConsoleApplication1{     class  program {        Static voidMain (string[] args) {Console.WriteLine ("Hello,world"); Othernamespace.a A =NewOthernamespace.a ();//In different namespaces in the same assembly internal can also be accessedconsoleapplication2.b B =NewConsoleapplication2.b ();The program where the//b is located is generated by another project, and Class B must be public to access it, meaning that you need to add a reference after you create a new program in VS. Console.ReadLine (); }    }} namespace othernamespace{    Internal  class A {         PublicA () {Console.WriteLine ("Here is A"); }    }}

3. C + +
There are three access control characters for C + +, Private,protected,public

Private can only be accessed within this class
Protected can only be accessed by this class and sub-class
Public can be accessed from anywhere

The private property cannot be inherited at the time of inheritance
If you use public inheritance, the properties of the member do not change
If you use protected inheritance, the public and protected of the parent class become protected in the child class.
If you use private inheritance, the public and protected of the parent class become private in the child class.

In contrast, Java and C # have only inherited the public.

In addition, C + + has a friend keyword that can be used to define friend functions and friend classes. A friend function is any member that is not included in any class that can access the class. The friend class destroys the encapsulation and concealment of the class.

The access control mechanism for C/C + + Java than teaching------

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.