OC_ inheritance, convenience constructors, initialization methods

Source: Internet
Author: User
Tags constructor inheritance reserved

Main.m

Oc3_ inheritance, convenience constructors, initialization methods

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "LuZhangZombie.h"

#import "TieTongZombie.h"



int main (int argc, const char * argv[]) {

Inheritance: Subclasses inherit all characteristics of the parent class and behave

When the object executes the method, the subclass overrides the method of the parent class, the method of the subclass is used, and if there is no override, the method of the parent class

Luzhangzombie *luzom =[[luzhangzombie alloc] init];

[Luzom walk];

[Luzom attack];

[Luzom loseequipment];

//

Tietongzombie *tiezom =[[tietongzombie alloc] init];

[Tiezom walk];

Inheritance is unidirectional, a class can have only one parent class, but a class may have countless subclasses

NSObject is the parent class of all classes, or it can be called a base class

The inheritance of a class is transitive

NSLog (@ "%@", [Tiezom class]);

C language If the operation against a null pointer, such as assignment, the system runs to this code directly crashes, and OC encountered a null pointer to the code as a scrap code, will execute, but will not have any effect

Zombie *zom =[zombie zombiewithblood:100 attack:20 speed:10];

[Zom attack];

return 0;

}

Zombie.h

Oc3_ inheritance, convenience constructors, initialization methods

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Zombie:nsobject


Characteristics

{

Nsinteger _blood;

Nsinteger _attack;

CGFloat _speed;

}





Behavior

-(void) walk;

-(void) attack;

-(void) dead;



-(ID) Initwithblood: (Nsinteger) Blood

Attack: (Nsinteger) attack

Speed: (cgfloat) speed;




Convenience Builder




+ (Zombie *) Zombiewithblood: (Nsinteger) Blood attack: (Nsinteger) Attack Speed: (cgfloat) speed;


+ (Zombie *) Zombiewithblood: (Nsinteger) Blood attack: (Nsinteger) Attack Speed: (cgfloat) speed;


+ (Zombie *) Zombiewithblood: (Nsinteger) Blood attack: (Nsinteger) Attack Speed: (cgfloat) speed;



@end

Zombie.m

Oc3_ inheritance, convenience constructors, initialization methods

//

Created by Dllo on 15/7/16.

Copyright (c) 2015 CML. All rights reserved.

//


#import "Zombie.h"


@implementation Zombie


-(void) walk{

NSLog (@ "Normal zombie is walking");

}

-(void) attack{

NSLog (@ "Normal zombie is attacking");

}

-(void) dead{

NSLog (@ "Normal zombie is dying");

}


Declaration of a custom initialization method

-(ID) Initwithblood: (Nsinteger) Blood

Attack: (Nsinteger) attack

Speed: (cgfloat) speed

{

The full version of the initialization method

The county invokes the initialization method of the parent class and sets the member variables inherited by the parent class.

Self =[super init];

When this object has a problem during initialization, a null pointer is returned, and if the next copy of the null pointer is meaningless, skip over and if a normal heap space pointer is returned, continue assigning the value

if (self) {

_speed =speed;

_blood =blood;

_attack=attack;

}

return self;

}



+ (Zombie *) Zombiewithblood: (Nsinteger) Blood attack: (Nsinteger) Attack Speed: (cgfloat) speed{

Class method

The convenience constructor puts the object creation process into the program's. m file, so that through the class, directly can create an object directly out

The System class provides a number of ways to facilitate the constructor


Zombie *zom = [[Zombie alloc] Initwithblood:blood attack:attack speed:speed];

return zom;

}



@end

Person.h

Oc3_ inheritance, convenience constructors, initialization methods

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Person:nsobject

3 Member variables

{

NSString *_name;

NSString *_sex;

Nsinteger _age;

}




Sayhi method

-(void) sayhi;



Set accessor

-(void) SetName: (NSString *) name;

-(void) Setsex: (NSString *) sex;

-(void) Setage: (Nsinteger) age;


-(NSString *) name;

-(NSString *) sex;

-(Nsinteger) age;


Custom initialization methods (3 member variables)

-(ID) Initwithname: (NSString *) name

Sex: (NSString *) Sex

Age: (Nsinteger) age;


Traverse Constructor (3 member variables)

+ (person *) Personwithname: (NSString *) name

Sex: (NSString *) Sex

Age: (NSString *) age;


@end

Person.m

Oc3_ inheritance, convenience constructors, initialization methods

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//


#import "Person.h"


@implementation person

-(void) sayhi{

NSLog (@ "%@,%@,%ld", _name, _sex, _age);

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.