Summary of polymorphism, point syntax and dynamic type in objective-c

Source: Internet
Author: User

I. Polymorphism

1. Polymorphism concept, definition

What is polymorphism: polymorphism is the multiple forms of a certain class of things;

Representation: Animal *ani = [Dog new];

Polymorphic conditions: 1. There is an inheritance relationship 2. Methods of rewriting

2. polymorphic Code Implementation:

Instantiate a Cat object

Animal *animal = [Cat new]; The pointer to the parent class points to the child class object;

[Animal eat];

Instantiating a Dog object

Animal = [Dog new]; The pointer to the parent class points to the child class object.

[Animal eat];

3. polymorphic Note points:

1) If there is polymorphism, the parent class can access the method unique to the child class

Suppose the subclass dog has a unique method bark

Animal *an2 = [Dog new]; Compile-time look at the left side of the equals sign and run to the right

[(dog*) an2 bark]; Converts a pointer to a parent class, coercion type

2) If there is no polymorphism, the parent class cannot access the subclass-specific method

Animal *an3 = [Animal new];

[(dog*) an3 bark]; False, cannot cast

Two. Class objects

1, the nature of the class is actually an object (class object);

Class object

* Class objects persist while the program is running.

* Class object is a data structure that stores basic information about a class: Class size, class name, version of the class, and the mapping table for messages and functions.

* Each object contains an Isa pointer to its class object.

2. How the Class object gets

Get by instance Object

Dog *D1 = [dog new];

Dog *D2 = [dog new];

Class C = [D1 class];

Class C2 = [D2 class];

Get by class name (the class name is actually a class object)

Class C3 = [Dog class];

Because the class object has only one copy in the entire program,

So c = C2 = C3

3. Use of Class objects

1) can be used to invoke the class method:

Get Class object

Class C1 = [Person class];

Invoking class methods using class objects

[C1 test]; Test is a + sign method

2) can be used to instantiate an instance object and call the object method

Get Class object

Class C1 = [Person class];

To create an instance object using a Class object

Person *p = [C1 new];

[P test]; -test Calling Object methods

4. Class object Storage Details

Analysis test by printing Address: Class object exists data segment;

5.SEL type

SEL is a new type of data. Same as the ID, class.

Person *p = [person new];

SEL S1 = @selector (test);

[P PERFORMSELECTOR:S1];

Three. dot syntax

1. Traditional setter, Getter method

-(void) Setage: (int) age{

_age = age;

}

-(int) age{

return _age;

}

2. Dot syntax

Essentially expanded after the equivalent call setter, getter method

P.age = 10;  After expansion is equivalent to [P setage:10]; Setter method

NSString *name = p.age; Equivalent to NSString *name = [p name]; Getter method

Object. After the member variable is expanded, is the setter method, or Getter method, to the left or right of the equals sign,

If it is the setter method to the left of the equals sign, the Getter method to the right of the equals sign

3. Dot Syntax traps:

Use self in the Set method.

-(void) Setage: (int) NewAge {

Self.age = NewAge;

}

2, point Grammar note

The essence of point syntax is the invocation of the method, not the access to the member variable, and the compiler automatically expands to the appropriate method when the point syntax is used.

Remember that the essence of point syntax is to convert to the appropriate set and get methods, and you cannot use point syntax without the set and get methods.

Four [email protected] and @synthesize

@property:

1, @property can only be written in the @interface @end.

2, @property the Get/set method declaration (xcode4.4 previously) used to automatically generate the member variable age.

3,xcode4.4 's subsequent enhancements not only helped us automatically generate Get/set methods, but also helped us automatically generate the implementation of the Get/set method.

4, tells the compiler that the member variable type to be generated by the Get/set method declaration is int.

5, if you do not manually declare member variables, Perperty will automatically help us generate a member variable at the beginning of the. m file

@synthesize:

1. @synthesize age; Represents the implementation of the Get and set methods of the variable age in the build. h

2. @synthesize assign a value to the specified instance variable.

@synthesize age = _age;

Note

If it is @synthesize, the variable name should be declared in the. h file first.

@property, @synthesize These two can be used together to simplify the definition and implementation of set and get methods.

Rewrite Setter, getter method:

Only the setter, or one of the getter methods, can be overridden

If you rewrite two, you will get an error.

Five. Dynamic type

1. Dynamic type:

Type of determination at run time

2. Dynamic binding:

Dynamic binding is based on the dynamic type:

Based on the dynamic type, the type is determined after an instance object is determined. The object corresponding to the property and the response message is also fully determined, which is dynamic binding.

Dynamic binding is done by binding certain properties and corresponding methods to the instance after the class of the instance is determined.

3. Dynamic Loading:

Dynamically create classes class, dynamically add Class member variables and member functions, dynamic variable assignment and value, dynamic function call, etc.

4. Introspection (introspection) is a powerful feature of object-oriented languages and environments, and objective-c and cocoa are particularly rich in this area.

Introspection is the ability of an object to reveal its own detailed information as a run-time object. These details include the location of the object on the inheritance tree, whether the object follows a specific protocol, and whether it can respond to a specific message.

1, Iskindofclass:class

Determines whether the instance object is a class or a subclass of this class.

Cases.

2, Ismemberofclass:class

Determine if this is an instance of this class.

3, + (BOOL) issubclassofclass:classobj

Determines whether the class is a subclass of the specified class.

4, Respondtoselector:selector

Determines whether an object can respond to an SEL

5, + (BOOL) Instancesrespondtoselector:

Determine if the class has this method. This method is a class method and cannot be used in a class object

6, Conformstoprotocol:protocol

Checks whether the object conforms to the protocol and implements all required methods in the protocol.

Dynamic Type detection:

1. Determine whether an object is an instance object of a class, or an instance object (object and Class) of a child class

Iskindofclass use format: [Object Iskindofclass class object];

BOOL isinstance = [ANI Iskindofclass:[animal class]];

2. Ismemberofclass determines whether an object is an instance object (object and Class) of the specified class

Format: [Object Ismemberofclass: Class Object]

BOOL isinstance = [Dog Ismemberofclass:[animal class]];

3. Determines whether a class is a subclass of a specified class (the relationship between classes and classes)

Format: [Class A Issubclassofclass: Class B];

BOOL issub = [Dog Issubclassofclass:[animal class]];

Summary of polymorphism, point syntax, and dynamic types in objective-c

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.