Inheritance, encapsulation, polymorphism of object-c

Source: Internet
Author: User

First, inheritance

Inheritance allows subclasses to derive some properties and existing methods from the parent class

@interface Classa:nsobject

@end

@implementation ClassA

@ end

You can also call a method of the parent class with super

Second, the package

1, four permission modifiers

@public, @protected, @private, @package

2, the attribute encapsulates the instance variable, the method encapsulates the concrete implementation code, the class encapsulates the property and the method. Subclasses can inherit methods from the parent class, and can override the (override) method of the parent class.

Three, polymorphic

1, support the polymorphism of the method name, does not support the parameter and operator polymorphism.

2, in the objective-c is through a selector called selector implementation. In Objective-c, selector has two meanings, used to refer to the name of the method when used in the source message to the object. It also refers to the unique identifier that replaces the method name after the source code is compiled. The type of the compiled selector is the same way that the SEL has the same name, and the same selector. You can use selectors to invoke methods of an object.

The selector has the following characteristics:

* All methods with the same name have the same picker

* All the pickers are not the same

(1) Sel and @selector

The type of selector is SEL. The @selector indicator is used to refer to the selector, and the return type is sel.

For example:

SEL Responsesel;

Responsesel = @selector (loaddatafortableview:);

You can get the picker through a string, for example:

Responsesel = nsselectorfromstring (@ "Loaddatafortableview:");

You can also get the method name by reverse conversion, for example:

NSString *methodname = Nsstringfromselector (Responsesel);

(2) method and Selector

The picker determines the method name, not the method implementation. This is the basis for polymorphism and dynamic binding, which makes it possible to send the same message to non-homogeneous objects, otherwise there is no difference between sending a message and calling a method in standard C, and it is impossible to support polymorphism and dynamic binding.

In addition, class methods of the same name and instance methods of the same class have the same picker.

(3) method return value and @ parameter type

The message mechanism finds the return value type and parameter type of the method through the picker, so dynamic binding (example: sending a message to an object defined by an ID) requires that the implementation of a method of the same name have the same return value type and the same parameter type; otherwise, the runtime may not find the corresponding method error.

There is one exception, although the same name and instance methods have the same picker, but they can have different parameter types and return value types.

Inheritance, encapsulation, polymorphism of object-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.