The concept and use of---class purpose of OC learning article

Source: Internet
Author: User

The previous article introduced the use of the @class keyword in oc http://blog.csdn.net/jiangwei0910410003/article/details/41774747, which we introduce, A unique highlight in OC: Category

First, let's take a look at the scenario, what should we do if we want to expand the functionality of a class now?

For object-oriented programming, inheritance is first thought, but there are two problems with inheritance:

The first problem: The premise of inheritance is that this class can be inherited, because some classes in Java are not allowed to inherit, defined as the final class, the same OC is also a class that can not inherit

The second problem: This class can be inherited, but the inheritance we know is intrusive, that is, I may just want to implement a function, but after inheritance, the subclass will inherit all the functions of the parent class (properties and methods), which is too expensive. No need.


So in Java, we can use aggregation to implement,

In the case of C + +, define a function class directly, and then let the target class inherit the function class, because C + + can inherit multiple

Java and OC are single-inheritance and do not allow multiple inheritance


In OC, of course, we can also use the same way as Java, but there is a more powerful processing in OC, that is, the class, look at the example: we now want to give the NSString Class A way to verify the mailbox Validateemail, The NSString class in OC is not inheritable.

Do not say, look directly at the code:

Nsstring+extension.h

  extension.h//  09_classtarget////  Created by Jiangwei on 14-10-11.//  Copyright (c) 2014 Jiangwei . All rights reserved.//#import <foundation/foundation.h>//Define class purpose file naming rules: Class name + class name, such as "Nsstring+extension.h" @ Interface NSString (Extension)-(BOOL) validateemail;//override NSString method in Intvalue-(Nsinteger) intvalue; @end

Nsstring+extension.m

  extension.m//  09_classtarget////  Created by Jiangwei on 14-10-11.//  Copyright (c) 2014 Jiangwei . All rights reserved.//#import "Nsstring+extension.h" @implementation nsstring (Extension)-(BOOL) validateemail{    Nsrange range = [self rangeofstring:@ "@"];    if (range.location = = nsnotfound) {        return NO;    } else{        return YES;}    } As can be seen here, there is no way to call NSString in the Intvalue method, which is different from the inheritance-(Nsinteger) intvalue{    NSLog (@ "Intvalue");    return 0;} @end
Here we define a category, as explained below:

The definition format of the category file:

The format of the class destination file name:"Add Class Purpose class name + Class purpose name"

For example here we need to add a class Extension to the NSString class, then the file name is: Nsstring+extension.h and NSSTRING+EXTENSION.M



Class Purpose Definition Format:

@interface need to add Class purpose class name (class name)

....

@end

This is the same as the class definition format, that is, there is more than one parenthesis class name

For example:

@interface NSString (Extension)

....

@end


The class name can be taken casually.


See the code above, in Nsstring+extension.h

We can define the Validateemail method, and we define a Intvalue method.

The Intvalue method is NSString itself, here is to demonstrate that if the class is redefined in the added class of the existing methods, then the method in the class will be overwritten, and the class inherits the same, but there is a point to note, Here is not the Super keyword to call NSString's Intvalue method, so Intvalue covered NSString class Intvalue method, NSString class Intvalue method The original function is not, This has to be noticed.


Check the test code.

Main.m

  main.m//  09_classtarget////  Created by Jiangwei on 14-10-11.//  Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> #import "Nsstring+extension.h"//This is just to demonstrate that if you want to extend the functionality of a class, Can be implemented through inheritance//But through inheritance to achieve this function, a bit overqualified feeling, because we only need a way to judge the legality of the mailbox, so the inheritance is big, no need//But here will be wrong, because NSString is not allowed to inherit// So here we are going to use a very powerful technology: class//class purpose definition and tired definition similar, in the. h file Declaration, the. m file implementation method//class can override the method, but will not execute the previous method, but call the new method//class can not add attributes (instance variable) int to class Main (int argc, const char * argv[]) {    @autoreleasepool {                NSString *s = @ "[email protected]";        BOOL Isemail = [s validateemail];        if (isemail) {            NSLog (@ "valid mailbox");        } else{            NSLog (@ "Invalid mailbox");        }                Called the Intvalue method in our class        nsinteger i = [s intvalue];        NSLog (@ "Intvalue is%i", i);    }    return 0;}
This allows us to add a method of verifying the mailbox to the NSString class, while the Intvalue method in the class overrides the Intvalue method in the original class.


With this example, we can see that the class in OC is really powerful!!

And the category actually has a hidden use, from his literal meaning to know the role of classification, such as a method in a class sometimes we may need to classify (such as: Print classification, comparative classification, etc.), so that the code looks very elegant. We can look at the Nsarray class in the foundation framework (which we'll talk about later, like the list in Java):


There is also an item of category.


Here we also learned that a class can be defined for multiple classes of purposes. And the purpose of the class can be inherited to subclasses, that is, the parent class of the method subclasses can still use the Super keyword to invoke.


Summarize

This article introduces a powerful functional category in OC, which plays an important role in the future extensibility, and is also considered as one of the key points in OC, and we will use this function frequently.

OC Learning---Concepts and use of the purpose

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.