Basic review: Get methods and Set methods (inheritance polymorphism for classes)

Source: Internet
Author: User

Basic review: Get methods and Set methods

When you define a class member variable, you can define it in @interface, or you can define it in @implementation:

Declared in @interface, the state of the member variable is protected, i.e. "@protected";

Declared in @implementation, the state of the member variable is private, that is, "@private"

Outside of the class, the member variables cannot be accessed directly, and can only be accessed externally if the member variable is modified to @public.

When using @public, Access member variables using "-", such as:

time->hour=25;

However, the use of @public is risky (as the above code sets hour to 25, actually only 24 hours), so the general access member variable uses the Get and set methods;

Declaration of the Get method:

-(int) hour;    Get method, naming rule: The return type is the type of the member variable, the method name is the member variable name

Declaration of the Set method:

-(void) Sethour: (int) Newhour    //set method, naming rule: The return type is void, the method name is the first letter of the set+ member variable name, with a parameter, the type of the parameter type bit member variable

Implementation of the Get method:

-(int) hour{    return hour;          }

The implementation of the Set method:

-(void) Sethour: (int) newhour{    if (newhour>24| | NEWHOUR<0)    //filter for unreasonable values    {         hour=0;    }    Hour=newhour;}

After you use the Get method and the Set method, you can use the "." Syntax Access member variables:

time.h=22;

“.” The syntax can call the Get method and the set method automatically, when assigning a value to the member variable, the Set method is called automatically, and when the member variable is obtained, the Get method is called automatically;

time.h=22;    Automatic call Set method assignment Xiaoshi=time.h    //Auto Call get method to get time.h value

In Objective-c, two statements are provided to automatically generate the Get and set methods:

@property: can only be used in @interface to automatically generate corresponding get and set method declarations for member variables

@synthesize: can only be used in @implementation to automatically generate corresponding get and set method implementations for member variables

For example:

@property int hour; @synthesize hour;
Advanced: Characteristics of @property and @synthesize

1. After using @property and @synthesize, you can still use Sethour: To access member variables:

Time *tm=[time new]; [TM Sethour:22];

2. After using @property and @synthsize, although the get and set methods are automatically generated, you can still redefine the set and get methods yourself, and the redefined method overrides the auto-generated method, which is convenient for filtering data and setting certain rules.

3. When the Get and set methods are automatically generated using only @property, @property automatically generates a member variable named "_ Name":

@property int hour; Automatically generate member variable "_hour"

At this point, the @property automatically performed 3 operations:

1) generate a member variable named "_hour" (after 4.6 version of Xcode)

2) generate a declaration of the corresponding get and set methods for "_hour"

3) Bit "_hour" generates the corresponding get and set method implementations

Therefore, after using @propery, you can omit the member variable declaration from "{}" because it is automatically generated.

[Email protected] hour, will look for instance variable "hour", if not found, automatically generate a "hour" member variable

Using: @synthesize Hour=_hour, you can associate "hour" with "_hour" and no longer generate "hour" member variables

5. In the set and get methods, use "self." is not allowed because "self." The call is the method itself, which forms a dead loop.

    • Member variables if external access is not required, use curly braces in the same way as before

Basic review: Get methods and Set methods (inheritance polymorphism for classes)

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.