Objective-c "Construction method (override, Scene, custom), super"

Source: Internet
Author: User
Tags rounds

———————————————————————————————————————————
Use of the Super keyword

#import <Foundation/Foundation.h>

@interface Animal:nsobject
-(void) run;
-(void) eat;
+ (void) eat;
@end

@implementation Animal
-(void) run
{
NSLog (@ "Animal run!");
}
-(void) Eat
{
NSLog (@ "-animal eat!-");
}
+ (void) eat//verified that super cannot refer to the parent class object, nor can it invoke the class method of the parent class through Super.
//{
NSLog (@ "+animal eat!+");
//}
@end

@interface Dog:animal
-(void) run;
@end

@implementation Dog
-(void) run
{
NSLog (@ "Dog run!");
[Super Run];
[Super Eat];
}
@end

@interface Bigyellowdog:dog
-(void) run;
@end

@implementation Bigyellowdog
-(void) run
{
NSLog (@ "Bigyellowdog run!");
[Super run];//called the Run method of the parent class (at this point the parent class of Bigyellowdog is Dog,dog's parent class is the Run method in both Animal,dog and animal, at which point the dog's Run method is called first), Super refers to the instance object of the parent class dog.
[Super eat];//called the Eat method of the parent class (at this time there is no Eat method in the dog class, and the animal class has, then the animal method in Eat is called), and Super refers to the instance object of animal, so super is a parent class that can refer to of the instance object.

★super refers to the instance object of the parent class. This sentence to understand, because run and eat are object methods, obviously need to have objects to invoke. So super refers to the instance object of the parent class.
}
@end

int main (int argc, const char * argv[]) {
@autoreleasepool {
Bigyellowdog *byd=[[bigyellowdog alloc] init];
[BYD Run];
}
return 0;
}


———————————————————————————————————————————
Overriding construction methods

#import <Foundation/Foundation.h>

@interface Person:nsobject
{
@public
int _age;
}
@end

@implementation person

Overriding the Init method so that the subclass overrides the Init method of the parent class when the call was made, and the purpose of the rewrite was achieved.
-(Instancetype) init
{
Self=[super Init];//init is an object method, and the return value is the Instancetype type (similar to the ID type). This place first calls the original parent class's Init method once, that is, the original Init method of the parent class is executed once. Why do you have to do this? That is because the parent class may fail to initialize during INIT, or it may produce other unknown initialization information. In order not to obscure what the parent does, our principle is to let the parent class do the things that were originally done, and then make a judgment that if the parent class Init succeeds (that is, not empty), then the overriding Init method is started
if (self)//if initialization succeeds
{
_age=10;//Setting the _age property of an instance object to 10
}
Return self;//finally returns the object (self refers to the caller of the method, which is the instance object itself)
}
@end

@interface Student:person
{
@public
int _sno;
}
@end

@implementation Student
-(Instancetype) init
{
If you do not write Self=[super init]; That is, do not do the initialization of the parent class, then output S->_age = 0, S->_sno = 1, which is why?
Obviously student is the subclass of person, we rewrite the Init method in person, let the instance object initialize the _age property value is 10, and student's init if only write _sno=1, obviously covered out the parent class _age initialization, Then the _age attribute is not initialized, which results in partial loss of the initialization, so strict procedures are adhered to when overriding the Init construction method:

① first executes the init of the parent class
Self=[super Init];
② determine if self is initialized successfully
if (self)
③ initializing an instance variable of the current class
{
_sno=1;
}
④return self; Returning an Instance object
return self;
}
@end

int main (int argc, const char * argv[]) {
@autoreleasepool {
Person *p1=[person new];
Person *p2=[[person alloc] init];
NSLog (@ "p1->_age =%d\np2->_age =%d", p1->_age,p2->_age);
NSLog (@ "**************************************************");

Student *s=[[student Alloc]init];
NSLog (@ "s->_age =%d\ns->_sno =%d", S->_age,s->_sno);
}
return 0;
}


———————————————————————————————————————————
Application scenarios for overriding construction methods

#import <Foundation/Foundation.h>

@interface Soldier:nsobject
@property gun* Gun;
-(void) fire;
@end

@implementation Soldier
Overriding the Init method for the soldier class, in order to create a soldier instance object each time, the object has a gun, so the gun property needs to be attached when initializing
-(Instancetype) init
{
if (Self=[super init])
{
Gun *gun=[[gun Alloc]init];
_gun=gun;
}
return self;
}
-(void) fire
{
[_gun shoot];
}
@end

@interface Gun:nsobject
@property int bulletcount;
-(void) shoot;
@end

@implementation Gun
Override the Init method for the gun class to create a gun instance object each time the object has 3 rounds, so the number of bullets is 3. This property needs to be attached when initializing
-(Instancetype) init
{
if (Self=[super init])
{
_bulletcount=3;
}
return self;
}
-(void) shoot
{
_bulletcount--;
NSLog (@ "SHOOT!!!!!! Remaining bullets:%d ", _bulletcount);
}
@end

int main (int argc, const char * argv[]) {
@autoreleasepool {
for (int i=0; i<20; i++) {
Soldier *s=[[soldier alloc]init];//created 20 soldiers, one for each gun, and three rounds for each, then three shots.
[s fire];
[s fire];
[s fire];
}
}
return 0;
}


———————————————————————————————————————————
Custom Construction Methods


Custom Construction methods:
We want to initialize with the specified value, for example, we initialize the student class, then we need to initialize the student's name, age, number, etc.
Precautions:
① the custom constructor method is an object method
② return value is instancetype type (ID)
③ method Name must be named Initwithxxxxx


#import <Foundation/Foundation.h>

@interface Person:nsobject
@property nsstring* name;
@property int age;
-(Instancetype) Initwithname: (NSString *) name andage: (int) age;
@end

@implementation person
-(Instancetype) Initwithname: (NSString *) name andage: (int) age//Our custom construction methods must be declared in the. h file
{
if (Self=[super init]) {
_name=name;
_age=age;
}
return self;
}
@end

@interface Student:person
@property int Sno;
-(Instancetype) Initwithname: (NSString *) name andage: (int) Age Andsno: (int) Sno;
@end

@implementation Student
-(Instancetype) Initwithname: (NSString *) name andage: (int) Age Andsno: (int) Sno
{
if (Self=[super initwithname:name andage:age]) {
_sno=sno;
}
return self;
}
@end

int main (int argc, const char * argv[]) {
@autoreleasepool {
Student *s=[[student alloc]initwithname:@ "Wang" andage:18 andsno:1];
NSLog (@ "name:%@,age:%d,sno:%d", S.name,s.age,s.sno);
}
return 0;
}


———————————————————————————————————————————

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c "Construction method (override, Scene, custom), super"

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.