Declares that the parent class animal inherits from NSObject
Animal.h
#ifndef oc_animal_h#define oc_animal_h@interface animal:nsobject { @public int weight;} -(void) eat;//override default constructor method-(ID) init;//Custom construction Method-(ID) initwithweight: (int) newweight; @end #endif
ANIMAL.M
#import <Foundation/Foundation.h> #import "Animal.h" @implementation animal-(void) Eat { NSLog (@ "Weight:%d", weight);} The ID is a universal pointer, can point to any object, do not add *//override the default constructor method of the Init constructor: Initialize Object-(ID) init{ if (self = [super init]) {//!=nil First initializes the parent object, Assign to self, then initialize your own member, and finally return self weight =; } return self;} Custom construction Method-(ID) initwithweight: (int) newweight{ if (self = [super init]) { self->weight = newweight; } return self;} @end
Subclass Dog, inherited from animal
Dog.h
#ifndef oc_dog_h#define oc_dog_h#import "Animal.h" @interface dog:animal { @public int speed;} @property int speed; Auto-Declare Get Set Method-(void) RUN;//ID is a universal pointer, can point to any object, do not add *//override Init constructor method: Initialize Object-(ID) init;//rewrite nsobject init-(ID) initwi Thweightspeed: (int) newweight: (int) newspeed;-(nsstring*) description; Object Description //rewrite NSObject description+ (nsstring*) description;//class object Description @end#endif
Dog.m
#import <Foundation/Foundation.h> #import "Dog.h" @implementation dog@synthesize speed; The compiler automatically generates a GET, set implementation Method-(void) Run { NSLog (@ "Run-speed:%d", tempo);} The ID is a universal pointer, which can point to any object, and do not add *//rewrite init constructor method: Initialize Object-(ID) init{ if (self = [super init]) {//!=nil speed =; Self->speed =; } return self;} Custom Construct method, call parent class custom construct-(ID) initwithweightspeed: (int) newweight: (int) newspeed{ if (self = [Super Initwithweight: Newweight]) {speed = newspeed; } return self;} -(nsstring*) Description//Object description //rewrite nsobject description{ return [NSString stringwithformat:@ "weight=%d, speed=%d ", weight, speed];} + (nsstring*) description//Class object Description { return [nsstring stringwithutf8string: "<Dog:Animal>"];} @end
main.m
#import "Animal.h" #import "Dog.h" int main () {animal* Animal = [[Animal alloc] init];//default construction [Animal eat]; Animal = [[Animal alloc] initwithweight:17]; Custom construction [Animal eat]; dog* Adog = [[Dog alloc] init];//default construction [Adog run]; [Adog eat]; Adog = [[Dog alloc] initwithweight:16];//The custom construction of the parent class [Adog eat]; Adog = [[Dog alloc] initwithweightspeed:16:13];//dog own custom construction [Adog run]; [Adog eat]; Polymorphic Creation Object animal* xdog = [[Dog alloc] initwithweightspeed:33:55]; [Xdog eat]; dog* Mdog = Xdog; [Mdog Run]; Description method call NSLog ([Mdog description]); Call object Method NSLog (@ "%@", mdog);//Output Object%@ Default Call Description Object method NSLog ([Dog description]); Call the class method NSLog ([[[[Dog class] description]); The default call to the Description class method omits the "%@" of format return 0;}
OC Language Learning (vi) inheritance, polymorphism, construction method, description method