Inheritance and class design, inheritance class design

Source: Internet
Author: User

Inheritance and class design, inheritance class design

======================================

Inheritance and Class Design

======================================

Oc is an object-oriented language. object-oriented programming has three main features: encapsulation, inheritance, and polymorphism.

 

1. Encapsulation

[Note] encapsulation is to modularize some code that solves some problems and expose an interface to the outside. We all call it encapsulation of a method;

[Advantages]

1. encapsulation can enhance code reusability.

2. encapsulation can effectively improve the development speed.

Ii. Inheritance

If the subclass has the methods and attributes of the parent class, we call it inheritance;

A: B (Reading: Class A inherits Class B, so Class A has class B's methods and attributes)

 

[Advantages]

1. Some functions can be implemented without writing any code;

2. You can extend the parent class without modifying the parent class method;

 

Iii. Polymorphism

Polymorphism: multiple forms of Classes

[Summary]

The method of polymorphism can effectively reduce the amount of code

Polymorphism is usually used in classes with the same method name. It is not clear which class to call before calling the method externally;

 

Iv. category (Extension)

 

There are some third-party classes, some of the classes that our programmers have previously written. When we need to use them, we suddenly find that if this class can have a xxx method, that would be fine, however, the third category or previously written class cannot be modified. So what should we do? You can use categories (extensions)

 

1. Recognize category

 

# Import <Foundation/Foundation. h>

# Import "dog. h"

// When using a category, you must first include the header file

// Categories are generally added to a single class for extension.

# Import "NSString + print. h"

Int main (int argc, const char * argv []) {

@ Autoreleasepool {

NSString * str = @ "hello world hahah ";

[Str show];

NSLog (@ "% @", str );

Dog * mydog = [[dog alloc] init];

[Mydog test];

}

Return 0;

}

# Import <Foundation/Foundation. h>

// NSString is the name of the class to be extended () inside the parentheses.

@ Interface NSString (print)

// Write the method to be extended for this location

-(Void) show; // print the current object

// Member methods can be extended, but member variables cannot be extended

// [Note] A category exists to extend a third-party class or other class methods previously written, but it cannot be modified by a third party or the method previously written;

@ End

 

# Import "NSString + print. h"

@ Implementation NSString (print)

-(Void) show

{

// Print the current object

NSLog (@ "% @", self );

}

@ End

 

# Import <Foundation/Foundation. h>

@ Interface dog: NSObject

-(Void) test;

@ End

 

# Import "dog. h"

# Import "NSString + print. h"

@ Implementation dog

-(Void) test

{

NSString * str = @ "I am a dog class ";

[Str show];

}

@ End

 

Differences between inheritance and category

1. Category is an extension of the class method, and member variables cannot be added. However, inheritance can be extended based on the original parent class and member variables.

2. Only new methods can be added to a category. Methods of the original class cannot be modified and deleted, but inheritance can be added or modified;

3. classes do not advocate overloading the original method, but inheritance is acceptable.

4. A category can be inherited. If a category is implemented in the parent class, this category also exists in the subclass.

 

2. Understanding extension is also a category;

 

[Knowledge expansion]

1. Methods or attribute variables declared in. h can be accessed by the outside world;

2. attribute variables or methods declared in. m are accessible only to the current class;

 

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.