Objective-c instance variable access level, objective-c instance

Source: Internet
Author: User

Objective-c instance variable access level, objective-c instance

In C # and JAVA, both method and variable have strict access level control, so the access level is rarely used in object-c, the reason may be that there is no access-level syntax on the method. There is no strength to control variables alone. Next we will discuss the access level of instance variables. When the instance variable is declared in the class, it is protected by default. Unlike C ++ or C #, java declares private by default. This is confusing for developers who develop in a multi-language environment.

The following describes the access level of instance variables:

Protection level Complier directive Desciption
Privete @ Private Use the method declared inside the class
Protected @ Protected Used inside the class and subclass
Public @ Public Use any classes and Methods
Package @ Package Used in any part of the package. Currently, it is only applicable to 64-bit systems.

 

 

 

 

 

Explicit and implicit Protection

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

@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 methods. However, the best practice is to explicitly write the access level, And you know its default access level instantly.

 

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.