OBJECTIVE-C Foundation: Fourth day-7. Class loading process

Source: Internet
Author: User

In the front, we know that the nature of the class is actually an object, is the class type, then the loading process is how? In fact, the loading process of the class is very simple, first load the parent class and then load the subclass, and each class will only be loaded once, let's look at the following ~



Example:

#import <Foundation/Foundation.h>

@interface person:nsobject
+ (void) load;
@end

@implementation person
+ (void) load
{
    NSLog (@ ' person-----load ');
}
@end

@interface Student:person
+ (void) load;
@end

@implementation Student
+ (void) load
{
    NSLog (@ "Student-----load");
}
@end

int main ()
{
    return 0;
}
In the main () function, there is no call to the class method set, load this class method is automatically called by the system, so we do not need to call ourselves.


Results:

2015-01-24 14:14:45.451 07-Nature of the class [10689:1160313] person----load
2015-01-24 14:14:45.452 07-The nature of the class [10,689:1,160,313] Student----Load



From the result, we can see that the system first loads the person and then loads the student.




And if we need to listen, when does it load? There is also a method called + (void) Initialize, let's take a look at:

#import <Foundation/Foundation.h>

@interface person:nsobject
+ (void) load;
@end

@implementation person
+ (void) load
{
    NSLog (@ ' person-----load ');
}
+ (void) Initialize
{
    NSLog (@ "person-initialize");
}
@end

@interface Student:person
+ (void) load;
@end

@implementation Student
+ (void) load
{
    NSLog (@ "Student-----load");
}
+ (void) Initialize
{
    NSLog (@ "student-initialize");
}
@end

int main ()
{
    [[Student alloc]init];
    return 0;
}


Results:

2015-01-24 14:24:18.588 07-Nature of the class [10729:1164951] person----load
2015-01-24 14:24:18.589 07-The nature of the class [10,729:1,164,951] Student----Load
2015-01-24 14:24:18.589 07-Nature of the class [10729:1164951] person-initialize
2015-01-24 14:24:18.590 07 -The nature of the class [10729:1164951] Student-initialize

If we change the main () function:

int main ()
{
	[[person alloc]init];
	return 0;
}

So the result is:

2015-01-24 14:25:17.760 07-Nature of the class [10738:1165590] person----load
2015-01-24 14:25:17.761 07-The nature of the class [10,738:1,165,590] Student----Load
2015-01-24 14:25:17.761 07-Nature of the class [10738:1165590] Person-initialize




The reason is very simple, in the main () function, if we have loaded into the subclass, then the Initialize method is from the parent class has been loaded into the child class, but if only the parent class, then the subclass will not be loaded.


The Load method is detailed: When the program starts, it loads all the classes in the project, and the class loads and then calls the + (void) Load method.
Initialize method: When using this class for the first time, the + (void) Initialize method is loaded.





OK, this time we'll talk about this, next time we continue to ~ ~

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.