OC-@ property, self, and class nature, oc-@ propertyself

Source: Internet
Author: User

OC-@ property, self, and class nature, oc-@ propertyself
Make code writing easier -- 1 -- seter and accesser1.1 setter1.2 getter-- 2 -- the nature of the class2.1 class objects2.2 category nature2.3 How to obtain class objects2.4 use of class objects2.5 storage of class objects-- 3 -- SEL type3.1 SEL-- 4 -- @ property keyword4.1 Basic Concepts4.2 @ property usage4.3 @ property precautions-- 5 -- @ synthesize keyword5.1 @ synthesize usage5.2 @ synthesize note5.3 @ synthesize: instance variable value assignment-- 6 -- self keyword6.1 Introduction6.2 use self in object Methods6.3 use self in class methods

 

 

[Written at the beginning]

"Encapsulation is an important feature of object-oriented programming. Encapsulation exposes an interface that is available for external use, hiding the complicated code logic implementation.

External Storage is the essence of object-oriented design, reducing the possibility of useless data.

Externally, you can set and Access Object Attributes by setter and getter, instead of directly accessing object attributes->

In the development process, considering the security requirements, the @ public, @ protected and other keywords are not used before the member variable name, but the Set method is used to provide the value of the member variable for the object, some unreasonable values can also be filtered and filtered within the set method. 』

 

 

-- 1 -- setter and accesser 1.1 setter

Setter method (setter): some unreasonable data filtering can be performed within the set method. Set Method: provides a method to set the value of member variables.

Naming rules:

1) The method name must start with set.

2) the name of the member variable followed by the set is capitalized.

3) the return value must be void.

4) Be sure to accept a parameter, and the parameter type must be consistent with the type of the member variable.

5) The name of the form parameter cannot be the same as that of the variable name (the name of the member variable starts with a _ line, and the name of the member variable is recommended by Apple to be distinguished)

Benefits:

1) Data is not exposed, ensuring data security

2) filter the Set Data

{// NSString * _ name; int _ age ;}

Setter

//setter- (void)setName:(NSString *)name;- (void)setAage:(int)age;

 

1.2 getter

Purpose: return the member variables in the object for the caller.

Naming rules:

1) There must be a return value. The type of the returned value is the same as that of the member variable.

2) The method name is the same as the member variable name for removing the underline (this is different from that for Java and other languages)

3) No parameters need to be received

//getter- (NSString *)name;- (int)age;

 

 

-- 2 -- the essence of a class: 2.1 class objects

The essence of a class is actually an object, but an object of the class type, also called a class object.

 

2.2 class objects

Class Object

* Class objects always exist when the program is running.

* A class object is a data structure with the basic information of the Storage Class: class size, class name, class version, and ing table between messages and functions.

* The information saved by the class object is determined during program compilation and loaded to the memory when the class is used for the first time.

* Class objects represent classes, and class objects represent class objects. class Methods belong to class objects.

* If the message receiver is a class name, the class name represents the class object.

* During runtime, all class instances are generated by class objects. class objects modify the isa value of the instance to their own address, and each instance's isa directs to the Class Object of the instance.

* You can know the information of the parent class and the methods that can be responded to from the class object.

* Class objects can only use class methods, but cannot use instance methods.

 

2.3 How to obtain class objects

1) Get Through Instance Object

Person * person = [[Person alloc] init]; // create an object // obtain the Class Object Dog class c1 = [person Class] through the instance object; // The Class type is a struct pointer Class c2 = [person class]; NSLog (@ "c1-> % p", c1 ); // 0x000011d8 NSLog (@ "c2-> % p", c2); // 0x000011d8

 

2) get through the class name (the class name is actually a Class Object)

// Obtain the Class Object class c3 = [Person Class] through the class name; NSLog (@ "c3-> % p", c3); // 0x000011d8

It is found that the object obtained through the class name and instance is the same

In fact, they all obtain the class Object Person class.

 

2.4 use of class objects

1) create an object with a Class Object

Two Testing Methods:

// Object method-(void) test; // class method + (void) test;

Use

// Use a class object to create the object Person * person2 = [[c1 alloc] init]; [person2 test]; // call the test-test of the Object Method

 

2) use class objects to call class methods

// Use a class object to call the class method [c1 test]; // call the class method + test

 

2.5 storage of class objects

Isa pointer

  • Each object contains an isa pointer. This Pointer Points to the class to which the current object belongs.
  • [P eat]; indicates to send an eat message to the object pointed to by p, and call the eat method of the object. At this time, the object will find the method stored in the class following the internal isa pointer, run.
  • Isa is a hidden pointer in an object, pointing to the class that creates this object.
  • Using the isa pointer, we can know the Class that the current object belongs to during running.

 

 

-- 3 -- SEL-type 3.1 SEL

SEL: Specifies the storage location of the method.

Method call principle: in fact, the methods for loading objects to the memory of each class are encapsulated into a series of SEL lists, and SEL stores the information of this method (including the memory address of the method, of course ), to call this method, the object will use the isa pointer to find the memory address of the corresponding method in the memory and then call this method, after this method is found, this class of object will put the SEL into the cache, so that the efficiency can be improved during the second search.

 

Person * p = [[Person alloc] init];

[P test];

Process of searching for methods

1) First, package the test method name into SEL-type data.

2) Find the corresponding method address based on SEL data

3) Call the corresponding method based on the method address

4) Note: There is a cache during this operation. The first search is performed one by one, which is very performance-consuming and will be directly used later.

 

 

-- 4 -- @ property keyword

4.1 Basic Concepts

@ Property is the instruction of the compiler. (Compiler instructions are completed by the compiler)

The advantage is that you do not need to manually write the code of the getter and setter methods of the member attributes.

 

4.2 @ property usage

Format:

@ Property type name method name

For example:

@ Property int age;

It completes the declaration and implementation of the set and get methods of the age attribute.

-(Void) setAge :( int) age;

-(Void) age;

Note:

1. Before Xcode4.4, it is used to implement the get/set method declaration.

2. After Xcode4.4, enhancements are available and the implementation is completed automatically.

Person. h->

/* If the declaration of the method is omitted, the following two statements are replaced: (void) setAge :( int) age;-(int) age. At the same time, the corresponding member attribute _ age is automatically generated, but this attribute is private and can only be accessed externally through setter/getter */@ property int age;

There is no custom _ age attribute in the Person class, but @ property automatically generates this attribute:

@ Implementation Person // object method-(void) test {NSLog (@ "-TEST"); self-> _ age = 3; // you can see here, @ property generates a _ age property} // class method + (void) test {NSLog (@ "+ TEST");} @ end

Call:

Person * person = [[Person alloc] init]; // create an object // here _ age property is generated by @ property person. age = 3; // set the NSLog (@ "person. age-> % d ", person. age); // 3

 

4.3 @ property precautions

In old-fashioned code

@ Property can only be written between @ interface... @ end

@ Property is used to automatically generate the declaration of the get/set Method of the member variable (before Xcode4.4)

Tell @ property that the member variable type declared by the get/set method to be generated is Declaration

Tell @ property the property of the get/set method to be generated, and remove the underline from the property name

 

 

-- 5 -- @ synthesize keyword

5.1 @ synthesize usage

@ Synthesize age;

Written in the. m file to implement the get and set methods of the variable age in generation. h.

Format:

@ Synthesize Method Name

Note:

If @ synthesize is used, the variable name must be declared in the. h file first.

. H

@ Property int age;

. M

@ Synthesize age;

The expanded format is as follows:

@ Synthesize age; // expand-(void) setAge :( int) age {self-> age = age; // note that the original member Variable _ age of Person is not assigned a value here, instead, a variable age is generated and assigned a value to it. // (if self is written here, the variable age is equivalent to a member variable.) // when @ synthesize is used, only the values of age and name are printed below, and _ age and _ name are not operated // NSLog (@ "age = % d, name = % @", age, name); // NSLog (@ "_ age = % d, _ name = % @", _ age, _ name );}

 

5.2 @ synthesize note

Before Xcode4.4, @ property and @ synthesize are used together to simplify the definition and implementation of the set and get methods.

 

5.3 @ synthesize: instance variable value assignment

Statement

@ Property int;

Implementation

 

@ Sythesize a = _ B; // use the get and set methods of a to modify the value of attribute B.

It is equivalent to the following code:

-(Void) setA :( int) {

_ B =;

}

-(Int) {

Reurn _ B;

}

@ Interface Person: NSObject @ property NSString * name; // if the two instance variables have the same type, for example, age and weight, @ property can be used to separate variables with commas, define @ property int age, weight; // test method-(void) test; @ end @ implementation Person on a row // use the getter and setter methods of age, modify the value of attribute _ age @ synthesize age = _ age, name = _ name, weight = _ weight; @ end

 

 

-- 6 -- self keyword 6.1

The self in the OC language is equivalent to the this pointer in C ++ and Java.

To learn how to use self, you must first understand the attribute concept.

What is the use of getter and setter methods?

The configurator and accessors provide a channel for external operation class internal attributes.

 

If this method is not used, how does the external operation operate the internal attributes of the class.

If these two methods are not provided, the value of this attribute cannot be changed by the outside world.

Because of the class property, the default value is @ protect (protected type ).

 

Self application scenarios

1) used in class methods

2) used in object Methods

3) Access member variables

4) Special Use of self in OC Memory Management

 

6.2 use self in object Methods
// Student learning object method-(void) study {// at this time, self refers to the student object NSLog (@ "-self-> % p", self); // 0x1002001b0}

 

6.3 use self in class methods
// Student information method + (void) info {// at this time, self refers to the student NSLog (@ "+ self-> % p", self); // 0x000013d0}

View:

@ Autoreleasepool {Student * stu = [[Student alloc] init]; // The Student object address NSLog (@ "stu-> % p", stu ); // 0x1002001b0 // Student address NSLog (@ "Student-% p", [Student class]); // 0x000013d0 // send study and info messages [Student info]; // 0x000013d0 [stu study]; // 0x1002001b0}

Output -->

 

Summary:

Who should self refer? It depends on the method in which self is written,

If self is written in the object method, it refers to the object of the current method ([class new]).

If self is written in the class method, it refers to the current class Object (class)

Note that class objects and objects are two concepts.

 

 

[Written at the end :]

"Think of a poem Xi Murong:

How can I meet you with a blooming tree? In my most beautiful moment, I have been in front of the Buddha for five hundred years, asking for Buddha. Let us settle a dusty Buddha, and finally let me grow into a tree for you? it's my past hope that when you walk closer, please listen to the trembling leaves, and when you finally walk past you with no respect my friends who fell down in the ground, that's not the petals, that's my withered heart 』

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.