Inheritance and class design

Source: Internet
Author: User

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

Inheritance and class design

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

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

I. Encapsulation

The "note" encapsulation is to modularize some of the code that solves some problems, exposing an interface to the outside. We are all called encapsulation of a method;

Advantages

1. Encapsulation can enhance the reusability of code.

2. Encapsulation can improve the development speed effectively.

Two. Inheritance

Subclasses have the methods and properties of the parent class, which we call inheritance;

A:B (read: Class A inherits Class B, and Class A has the methods and properties of Class B)

Advantages

1. Do not write any code, you can achieve some functions;

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

Three. polymorphic

Polymorphism: Multiple forms of a class

Summary

Polymorphic way, can effectively reduce the amount of code

Polymorphism is used in classes with the same method name, and external calls to the method do not clearly know which class method to call;

Four. Categories (category) (extended)

There are some third-party classes, some of our programmers used to write their own classes, to use the time suddenly found that if the class can have a XXX method that would be good, but do not allow the third category or the previously written class to modify. So what? You can use the category (extension)

1. Recognize category

#import <Foundation/Foundation.h>

#import "Dog.h"

You need to include the header file when you use the category

Categories are generally placed in a single class to extend

#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 class to extend () the inside of the parentheses is the class name you write yourself

@interface NSString (print)

This position is written in a way that needs to be expanded.

-(void) show; Print current Object

You can extend member methods, but you cannot extend member variables

The "note" category is to extend the third party class or other previously written methods of the class, but can not be the third party and previously written methods to modify, and exist;

@end

#import "Nsstring+print.h"

@implementation NSString (print)

-(void) show

{

Print 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 dog class";

[STR show];

}

@end

Differences in inheritance and categories

1. Categories are extensions to the methods of a class and cannot add member variables. However, inheritance can extend the methods and member variables on the basis of the original parent class.

2. Categories can only add new methods, cannot modify the method of deleting the original class, but the inheritance can be added and modified;

3. Classes do not advocate overloading the original method, but inheritance is possible.

4. The category can be inherited, and if the class is implemented in the parent class, the subclass also exists in this category.

2. Recognition of extension (extension) is also a category;

"Knowledge extension"

1. Declare a method or attribute variable in. h that can be accessed by the outside world;

2. Declare the attribute variable or method in. m, except that the current class can be accessed;

Inheritance and class design

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.