Objective-c the access level of the instance variable

Source: Internet
Author: User

In C # and Java, both method and variable have strict access level control, so the use of access levels in Object-c is very rare, perhaps because there is no access level syntax on method, there is nothing to control variables alone. Let's discuss the level of access to the instance variable below. When an instance variable is declared in a class, the default is protected, unlike the general C + + or C#,java default declaration, which is private. This is confusing for those of us who develop in a multi-lingual environment.

The following describes the access levels for the following instance variables:

Protection level complier directive Desciption
Privete @private Methods declared inside a class use the
Protected @protected Use inside this class and subclass
Public @public To use from any class and method
Package @package Used anywhere within the package, currently only available on 64-bit systems

Explicit protection and implicit protection

Because protected is the default access level, you can declare it explicitly.

@interface Player:nsobject{//this is  a protected instance varible that implicit declareint m_playerexperience;} @interface Player:nsobject{//this is  a protected instance varible that explicit declare@prootected int m_ Playerexperience;}

There is no difference between the two ways. But the best practice is to write the access level explicitly, and immediately you know its default access level.

Access level of the OBJECTIVE-C instance variable

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.