Objective-c inheritance, initialization method, convenience builder

Source: Internet
Author: User

Inheritance, initialization method, convenience builder

Today we are going to learn to inherit the initialization method and Traverse The constructor first we have to understand some conceptual things .

Inheritance : In oc , inheritance is a single inheritance , so-called single inheritance is a subclass inheriting a parent class , for example, the person class we created earlier is inherited from nsobject . Review the inherited format

@interface Person:nsobject//After the Colon is the inherited parent class. human inheritance and NSObject

A subclass can inherit only one parent class , but the parent class may have more subclasses . For example :

@interface Student:person// students inherit from person class

@interface Teacher:person// teachers inherit with the person class teacher, the students are all human

The use of inheritance : can greatly reduce the amount of code , but also the sub-class has a common denominator to a piece , a common attribute can be written in the parent class , when used , the subclass can be called directly .

For example : Human teacher and student They will all be by name name sex sex age age and other common attributes so that it is written in the parent class The span class= "S1" >person can be

Attributes , which are written separately in their own classes . For example, the teacher has a salary , students pay tuition fees so different attributes .

@interface Person:nsobject

{

The following three examples are shared by both teachers and students , so they are written in the parent class they inherit .

_name = name;

_sex = sex;

_age = age;

}

@interface Teacher:person

{

The salary is the property that the teacher owns separately , therefore writes in the teacher class

_salary = salary;

}

@interface Student:person

{

Tuition is a separate attribute for students , so it is written in the student class .

_schooling = schooling;

}

Initialization method:

Aswe all know, Objective-c is an object-oriented language, in general, when we define a class in objective-c, we always provide an initialization method, which is generally written by everyone:

-(ID) init

{

self = [super init];

if (self) {

Perform initialization of some resources and variables

}

return self;

}

This is a simple code, but there are a lot of things to think about:

1. Why should I invoke the initialization method of the parent class through [super Init] and what is being done in the initialization method of the parent class?

First of all, we know the concept of object inheritance, a subclass inherits from the parent class, so we also implement all the functions of the parent class, which is Is-a's relationship, such as the teacher mentioned above (Teacher) and

Students (Student) are people, then teacher and Student are all back to the character and function of person. Therefore, in the initialization method of the subclass, the initialization method of the parent class must be called First,

To implement the initialization of the parent class-related resources. For example, when initializing a Teacher object, we must first initialize the object of the person class and assign the result to Teacher so that Teacher

meet The character of the person. Typically, under iOS, all classes inherit from NSObject, and NSObject's init method is simple, which is return self. When the initialization of the parent class is complete

after that, self is not nil. , you can start to initialize the subclass.

2, whether must provide initialization method, whether must use init as initialization method?

We create an object in objective-c typically using the

Person *per = [[Person alloc] init];

Or

Person *per = [person new];

The new method is a static method of the NSObject object, which, according to Apple's documentation, is actually a combination of the alloc and init methods, which is actually the same, but Apple still pushes

Recommend us to use the first method, why? Because using the first method, you can use your own defined init method to do some initialization, of course, if the subclass does not provide the Init method, since

The Init method of the parent class is called . So, from a security point of view, as a developer we must initialize the object before the object is used, so when defining the class

Be sure to provide a method of initialization. But are you sure you want to use init as the method name? The answer is not necessarily. Using init as the method name is just a rewrite of the NSObject init method, if

It is perfectly possible to redefine the initialization method yourself, as long as you remember to invoke the new defined initialization method when you are using it.

However, this approach is not advisable from a design standpoint. In terms of reusability, if it is necessary to define some initialization methods that accept different parameters, my

First, define a common method for init and call it in other methods, such as:

-(ID) init

{

self = [super init];

if (self) {

}

return self;

}

-(ID) Initwithname: (NSString *) name

{

[Self init];

_name = name;

}

-(ID) Initwithage: (Nsinteger) Age

{

[Self init];

_age = age;

}

Add:

In object-oriented programming, if you write a class without a constructor, the class can compile and work perfectly. If the class does not provide an explicit constructor, compile the

will provide you with a default constructor. In addition to creating the object itself, the only function of the default constructor is to call its superclass's constructor. In many cases, this superclass is a language box

Part of the frame, such as the object class in Java, the NSObject class in Objective-c.

In either case, having at least one constructor in a class is a good programming practice, and if there are attributes in the class, it is often good practice to initialize those properties.

-- above from the object-oriented thought Process byMatt Weisfeld

Person *per = [[Person alloc] Init];//alloc is a class method, Init is an instance method, Person Alloc creates an object (per), and then calls per Initializes the object by initializing the object's Init method.

-[(ID) Initwithname: (NSString *) name

Age: (Nsinteger) Age

{

if (self = [super init])

this is not to determine if self is equal to [super init] , but to determine if it can be initialized successfully. and if judged, is a bit of the way self is empty case

_name = name;

_age = age;

}

+ (ID) personwithname: (NSString *) name

Age: (Nsinteger) Age

{

ID obj = [[Person alloc] initwithname:name age:age];

set the value of obj

return obj;

}

Calling Methods

Person *per = [Person alloc]personwithname:@ "Jack" age:30];

This method calls the above + number class method , that is, the initialization method of the convenience constructor

Objective-c inheritance, initialization method, convenience builder

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.