Set functions, get functions, point syntax, and class methods

Source: Internet
Author: User

1. Set function

A setter function that assigns a value to a member variable. The general wording of the SET function is to use the age operation as an example:

-(void) Setage: (int) newage.

2. Get function

The Getter function, which takes a value on the member variable. The general wording of the Get function is also an example of the operation of age , as follows:

-(int) age;

3. The dot operator cannot access member variables in OC class objects, such as dog.age=5, where member variable age is not used, but instead calls the member method Setage, which is equivalent to assigning a value to Dog.age when the method [dog Setage:5].

NSLog (@ "%d", dog.age); This is also not the case with the member variable age, but the call to the member method, when the value of the dog.age expression is used, is actually called the member method, [dog age].

"." You cannot call a member variable, just a shorthand for calling a set function and a Get function. It is not understood here that the get function and the set function are defined to invoke member variables, because when you use Dog.age, you will get an error when you write the two functions in a normal form .

4. @property and @synthesize

When defining a set function and a get function, we will find that many of the member variable definitions will do a lot of repetitive work,oc for convenience, to encapsulate this method, we can use @property to declare the set and get function, Use the @synthesize to implement the set function and the get function. Also take the member variable age of the dog class as an example.

-(void) Setage: (int) newage;

-(int) age;

These two functions use @property, which can be written as "@property int age;", which greatly reduces repetitive work.

A post-declaration implementation function is typically implemented as follows:

-(void) Setage: (int) newage

{

Age=newage;

}

-(int) age

{

return age;

}

The same @synthesize can be simplified to "@synthesize age;"

5. The name of the set and get functions is different from the variable name, but the point syntax is used at the same time

The 1 function differs from the variable name by using the @property and @synthesize

@property Secondnum;

@synthesize secondnum=b;

When the implementation of the Association, the Declaration of time without a tube. At this point, you can use "Object . Sencondnum" to assign and take a value for B.

2 the Set function and the get function name do not correspond

Change "@property int D;" to "@property (getter = d1st) int D;"

"@synthesize D" is not changed, the assignment is different from the invocation, and the assignment is "object . D=1", which is "Object . d1st" When the value is evaluated. This just adds a new name, not a substitute for the original "object . D=1", but [object SetA] cannot be used.

3 @property (setter=set2nd:; getter=d1st) int D;

Here the getter and setter functions are added to the new name, it must be noted that the name of theset function must be "setxxxx:", must not forget the colon, separated by commas get function, the two functions are not separated.

"@synthesize D;" does not change.

4 @property (readonly) int D;

The contents of the () are called Property properties, and if the property is ReadOnly, only the Get function is declared, the set function is not declared , and the default is ReadWrite. The set function is not implemented without declaring the Set function @synthesize .

6. "+" and "-" before function

Common function names in OC are preceded by "+" and "-", where the plus minus sign represents different method types.

+ functions that represent functions as classes, called with the class name.

-functions that represent functions as objects are called when called.

The class does not have space, there is no space for member variables, and the methods of the class cannot use the member variables of the object.

Useful: 1 When there is no object, call the + function to create an object.

2 Single-case mode. Can be viewed as an upgraded version of the global variable. The object of one class is common to all objects of other classes. (The next stage deepens!!) )

Set functions, get functions, point syntax, and class methods

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.