First, the implementation of inheritance in Objective C
In OC, only instance variables will have permission control, instance methods and class methods are not permission control, this is different from C + +, OC default is protected, and when declaring permission control, there is no semicolon
You can use pointer arithmetic to access instance variables in OC like C + +
Rectangle.h File Code:
#import <Foundation/Foundation.h>
@interface Rectangle:nsobject
{
int _width;
int _height;
}
@property (nonatomic,assign) int width;
@property (nonatomic,assign) int height;
-(Rectangle *) Initwithwidth: (int) W andheight: (int) H;
-(void) print;
@end
RECTANGLE.M File Code:
#import "Rectangle.h"
@implementation Rectangle
@synthesize Width=_width;
@synthesize Height=_height;
-(Rectangle *) Initwithwidth: (int) W andheight: (int) H
{
Self=[super Init];
if (self)
{
Self.width=w;
Self.height=h;
}
return self;
}
-(void) print
{
NSLog (@ "The height is%d, the width is%d", self.height,self.width);
}
@end
Square.h File Code:
#import "Rectangle.h"
@interface Square:rectangle
{
int _d;
}
-(Square *) Initsquarewithwidth: (int) W andheight: (int) H andval: (int) D;
@property (nonatomic,assign) int D;
@end
SQUARE.M File Code:
#import "Square.h"
@implementation Square
@synthesize d=_d;
-(square*) Initsquarewithwidth: (int) W andheight: (int) H andval: (int) d
{
Self=[super Init];
if (self)
{
Self.height=h;
Self.width=w;
Self.d=d;
}
return self;
}
-(void) print//implement Overloads
{
NSLog (@ "The D is%d,the width was%d,the height is%d", self.d,self.width,self.height);
}
@
Main function:
#import <Foundation/Foundation.h>
#import "Access.h"
#import "Rectangle.h"
#import "Square.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Rectangle *test=[[rectangle alloc] init];
[Test Initwithwidth:3 Andheight:4];
[Test print];
Square *squareobj=[[square alloc] init];
[Squareobj initsquarewithwidth:3 andheight:4 Andval:5];
[Squareobj print];
}
return 0;
}
Second, the implementation of Objective C protocol
It is not possible to implement multiple inheritance in OC because it is too expensive to implement multiple inheritance, but OC can implement multiple protocols, similar to pure virtual classes in C + +, only method definitions, no method implementations, only. h files, no member variables.
Protocol (protocal) is the same as Java Interface (interface) and C + + pure virtual class, the protocol is only responsible for the list of methods, is not responsible for the implementation of the method, the purpose is to let other classes to implement, the Protocol only the interface part, no m file, the keyword is protocol, The protocol can inherit other protocols, and the protocol cannot define other member variables.
The protocol itself is not a class, it defines an interface that other classes can implement
The protocol can be implemented by any class.
Keyword of the agreement:
@required: Represents a method that must be enforced
@optional: Represents a method that can be implemented selectively
Graphics.h file
#import <Foundation/Foundation.h>
@protocol Graphics
-(void) OnDraw;
@end
Implementation of the Ellipse class
#import <Foundation/Foundation.h>
#import "Graphics.h"
@interface Ellipse:nsobject <Graphics>
@end
#import "Ellipse.h"
@implementation Ellipse
-(void) OnDraw
{
NSLog (@ "Draw ellipse");
}
@end
Implementation of the Triangle class
#import <Foundation/Foundation.h>
#import "Graphics.h"
@interface Triangle:nsobject <Graphics>
@end
#import "Triangle.h"
@implementation Triangle
-(void) OnDraw
{
NSLog (@ "draw triangle");
}
@end
The implementation of main function
int main (int argc, const char * argv[])
{
@autoreleasepool {
ID test;
Test=[[ellipse alloc] init];
[Test OnDraw];
[Test release];
Test=[[triangle Alloc]init];
[Test OnDraw];
[Test release];
}
return 0;
}
The RESUTL is:
Draw Ellipse
Draw Triangle
Objective C's inheritance is a single inheritance, but it can implement multiple protocols, using, splitting
Third, the realization of classification in OBJECTIVE-C
The classification mechanism allows the addition of a new method declaration to a class file, which does not require the use of a subclass mechanism, and defines these methods under the same name in the file implemented by the class.
Define the method:
#interface class name (category) as long as the classification is marked, you can add different classification methods
Disadvantages of the classification mechanism:
1. Cannot add attribute, only Add method
2. Name conflicts, if a method in a category has the same name as an existing method, the category has a higher priority when a naming conflict occurs
Benefits of the classification mechanism:
1. Spread the implementation of the class into multiple different files or multiple frameworks to implement
2. Creating a forward reference to a private method
3. Object-oriented Add informal protocol
#import "ClassName.h"
@interface ClassName (CategoryName)
@end
Vector.h Code
#import <Foundation/Foundation.h>
@interface Vector:nsobject
{
Double _vec1;
Double _vec2;
}
@property (nonatomic,assign) double vec1;
@property (nonatomic,assign) double vec2;
-(Vector *) add: (vector *) v;
-(void) SETVEC1: (double) vec1 AndVec2: (double) vec2;
-(void) print;
@end
VECTOR.M Code
#import "Vector.h"
@implementation Vector
@synthesize VEC1=_VEC1;
@synthesize vec2=_vec2;
-(void) print
{
NSLog (@ "V1 value is%f,v2 value is%f", SELF.VEC1,SELF.VEC2);
}
-(void) SETVEC1: (double) vec1 AndVec2: (double) vec2
{
SELF.VEC1=VEC1;
SELF.VEC2=VEC2;
}
-(vector*) Add: (Vector *) v
{
Vector *vector=[[vector Alloc]init];
VECTOR.VEC1=V.VEC1+SELF.VEC1;
VECTOR.VEC2=V.VEC2+SELF.VEC2;
return vector;
}
@end
Vector+sub.h Code
#import <Foundation/Foundation.h>
#import "Vector.h"
@interface Vector (sub)
-(Vector *) Sub: (vector *) v;
@end
VECTOR+SUB.M Code
#import "Vector+sub.h"
@implementation Vector (sub)
-(Vector *) Sub: (Vector *) v
{
Vector *temp=[[vector alloc]init];
TEMP.VEC1=SELF.VEC1-V.VEC1;
TEMP.VEC2=SELF.VEC2-V.VEC2;
return temp;
}
@end
Main code:
int main (int argc, const char * argv[])
{
@autoreleasepool {
Vector *test1=[[vector Alloc]init];
[Test1 setvec1:2.2 andvec2:3.4];
[Test1 print];
Vector *test2=[[vector Alloc]init];
[Test2 setvec1:1.1 andvec2:1.2];
Vector *ret=[test1 Sub:test2];
[RET print];
[Test1 release];
[Test2 release];
}
return 0;
}
Result
2013-05-06 17:38:34.751 access[2057:303] v1 value is 2.200000,v2 value is 3.400000
2013-05-06 17:38:34.753 access[2057:303] v1 value is 1.100000,v2 value is 2.200000
If you want to add more functions to a class by category, sample:
Nsstring+utilities.h
#import <Foundation/Foundation.h>
@interface NSString (Utilities)
-(BOOL) Isurl;
@end
#import "Nsstring+utilities.h"
@implementation NSString (Utilities)
-(BOOL) Isurl
{
if ([Self hasprefix:@ "http"])
{
return TRUE;
}else
{
return FALSE;
}
}
@end
This allows you to add a number of functions
Classification is not the concept of C + +, the use of classification by OBJECTIVE-C dynamic binding implementation, through classification can achieve better than inheritance effect
The realization of polymorphism in the Quaternary and objective C
Dynamic type: OC determines the actual type of the object at run time, such as ID car, at which time only we know its actual type when executing to the statement
Dynamic binding: The actual method of object invocation is determined by the program at execution time
Types that can be determined at compile time are static types, such as student *stu; This is a static type
Graphics.h File Code:
#import <Foundation/Foundation.h>
@interface Graphics:nsobject
-(void) OnDraw;
@end
GRAPHICS.M File Code:
#import "Graphics.h"
@implementation Graphics
@end
Triangle.h Code:
#import "Graphics.h"
@interface Triangle:graphics
@end
TRIANGLE.M Code:
#import "Triangle.h"
@implementation Triangle
-(void) OnDraw
{
NSLog (@ "draw triangle");
}
@end
Ellipse.h Code:
#import "Graphics.h"
@interface Ellipse:graphics
@end
ELLIPSE.M Code:
#import "Ellipse.h"
@implementation Ellipse
-(void) OnDraw
{
NSLog (@ "Draw ellips");
}
@end
Main function:
int main (int argc, const char * argv[])
{
@autoreleasepool {
Graphics *graphics;
Graphics=[[ellipse alloc] init];
[Graphics OnDraw];
[Graphics release];
Graphics=[[triangle alloc] init];
[Graphics OnDraw];
[Graphics release];
ID test;
Test=[[ellipse alloc] init];
[Test OnDraw];
[Test release];
Test=[[triangle alloc] init];
[Test OnDraw];
[Test release];
}
return 0;
}
Output Result:
2013-05-05 19:25:05.941 access[966:303] Draw Ellips
2013-05-05 19:25:05.943 access[966:303] Draw triangle
2013-05-05 19:25:05.944 access[966:303] Draw Ellips
2013-05-05 19:25:05.944 access[966:303] Draw triangle
The ID type is one of the three most commonly used iOS types, and the other includes: base type and object type, and ID type can represent any object type, which is commonly used to implement polymorphic
Implementation of inheritance, protocol, classification and polymorphism in Objective C