OC variable scope

Source: Internet
Author: User

# Import <Foundation/Foundation. h>

@ Interface person: nsobject

{

Int _ age; // The default value of the variable in @ interface is @ public.

@ Protected

Int _ height; // It can only be accessed in the object methods of the current class and subclass.

@ Pritate

Int _ weight; // can be accessed directly only in the object method of the current class

@ Package

Nsstring * _ name; // it can only be used within the current framework

}

-(INT) age;

-(Void) setage :( INT) age;

-(INT) height;

-(Void) setheight :( INT) height;

-(INT) weight;

-(Void) setweight :( INT) weight;

-(Nsstring *) Name;

-(Void) setname :( nsstring *) Name;

@ End


@ Implementation person

{

Nsstring * birthday; // The default value of the variable in @ implementation is @ private.

}

-(INT) Age

{

Return-age

}

-(Void) setage :( INT) Age

{

-Age = age;

}

-(INT) Height

{

Return _ height;

}

-(Void) setheight :( INT) Height

{

-Height = height;

}

-(INT) Weight

{

Return _ weight;

}

-(Void) setweight :( INT) Weight

{

_ Weight = weight;

}

-(Nsstring *) Name

{

Return _ name;

}

-(Void) setname :( nsstring *) Name

{

_ Name = Name;


}


@ End



1 @ public (public) can be directly accessed anywhere on the premise of an object.

2 @ protected (protected) can only be accessed in the object methods of the current class and subclass

3 @ private (private) can only be accessed directly in the object method of the current class

4 @ package (Framework-level) the scope is between private and public. You can directly ask by variable name as long as you are in the same framework.

5 @ InterfaceBy default, the declared member variables are public, and the member variables declared in @ implatation are private.




 

OC variable scope

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.