A simple understanding of object-oriented

Source: Internet
Author: User

Object-oriented programming, OOP, is a programming paradigm that satisfies the language of object-oriented programming and generally provides classes, encapsulation, inheritance and other syntax and concepts to assist us in object-oriented programming.

Object-oriented is based on the philosophical view that everything is object. The so-called object-oriented is to put our program into a modular, object, the specific characteristics of the properties of things and through these properties to implement some of the specific methods of action into a class inside

Three characteristics of object-oriented inheritance, encapsulation, polymorphism

Inherited

Inheritance: Subclasses have all the properties, and methods, of the parent class.

Benefits of Inheritance: extracting duplicated code, reducing code redundancy

Disadvantages of inheritance: coupling is too strong

use of inheritance note

(1) The name of a member variable with the same name is not allowed in OC and the parent class;

(2) The subclass in OC can have the same name as the parent class method, in the subclass call, take precedence of their own internal search, if not the first layer of the upward looking;

Overriding a method of a parent class in a subclass overrides the method of the parent class.

Super Keyword:

The Super keyword, when overriding a method in a subclass, allows the caller to skip this layer and invoke a method in the parent class.

Usage Scenario: Subclasses want to preserve some behavior of the parent class when overriding the parent class method.

Packaging

Concept: Encapsulation, also known as information hiding, refers to the use of abstract data types to encapsulate data and data-based operations together to form an indivisible, independent entity, where data is protected within the abstract data type, hiding internal details as much as possible, while preserving some external interfaces to make them externally related. The rest of the system is communicated and interacted with this abstract data type only through authorized operations that are wrapped outside of the data. That is, the user does not need to know the implementation details of an object's internal methods, but can access the object based on the external interface (object name and parameters) provided by the object.

Benefits: (1) achieve a professional division of labor. Once the code that implements a particular function is encapsulated into a separate entity, the programmer can invoke it when needed, thus achieving a professional division of labor. (2) Hide the information and implement the details. By controlling access permissions, you can hide information that you do not want the client programmer to see, such as a customer's bank password that needs to be kept secret and can only develop permissions for that customer.

Polymorphic:

Without inheritance, there is no polymorphism.

Methods for objects of different classes to define the same name

When a pointer or reference to a parent class object is directed to a subclass object, it is dynamically monitored to invoke the real method

C + + has only virtual functions to achieve polymorphism, and all the methods in OC can

Understanding: Subclasses appear as parent, but do things in their own way. Subclasses appear as parent classes need to be transformed (upcast), where the upward transformation is automatically implemented by the JVM and is secure, but the downward transition (downcast) is unsafe and requires casting. When a subclass appears as a parent, its own properties and methods cannot be used.

And OC is a typical object-oriented language.

OC is also a dynamic language, so how does his dynamics manifest?

Let's look at what a dynamic language is.

Dynamic language means that a program can change its structure at run time: New functions can be introduced, existing functions can be deleted, and so on structure changes

For example, the well-known ECMAScript (JavaScript) is a dynamic language. In addition, such as Ruby, Python and so on are also dynamic languages, and C, C + + and other languages are not dynamic language.

The dynamic characteristics of OC are represented in three aspects: dynamic type, dynamic binding, dynamic loading. It's called dynamic because it has to be done at runtime (run time).

Dynamic type: The program cannot determine which class it belongs to until it executes. dynamic type, say simple point is ID type

ID data type

An ID-generic object type that can store an object of any type, with no * number behind the ID, which is itself a pointer

Similar to void *, but can only point to object type

Dynamic binding: The program will not determine the actual method to invoke until it executes.

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

Class: Represents a class name, and when class is created, we can take class as the class of the object.

Class cla1 = [classes name]

Class CLA2 = [Object class]

Class Cla3 = Nsclassfromstring (@ "class name");

A pointer to the SEL class member method

It can be understood that @selector () is the number of the class method, his behavior is basically equivalent to the C language of the function pointer, except that the C language, can be directly assigned to a function pointer, and the OBJECT-C class cannot directly apply the function pointer, This can only be done with a @selector syntax to fetch.

The result is an SEL type. The type essence is the number of the class method (function Address)

The methods inside the 1> class are converted to SEL variables for storage.

2> A class declares an object, when the object invokes the method, the system is converted to SEL by this method, and then the SEL is matched to the class method.

3> we can manually convert the method to SEL, then use this sel to find the method (Performselector).

-ismemberofclass:

Determine if it is an instance of this class

-iskindofclass:

Determines whether this class or instance of a subclass of this class

-respondstoselector:

Is there such a way to interpret an instance

+instancesrespondtoselector:

Determine if the class has this method. This method is a class method.

Dynamic loading

It is easy to understand that the required resources are loaded on demand, which is basically adapted to different models for iOS development. The most classic example is the loading of @2x images on the retina device, and the loading of the original image on an older normal screen device. With the launch of the retina ipad and the advent of possible Retina Macs, this feature is believed to be increasingly being used.

A simple understanding of object-oriented

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.