Objective-c language polymorphism

Source: Internet
Author: User
Tags sca scalar



Dynamic type and dynamic binding, ID can represent any pointer type, definition ID variable not added *



Polymorphism means that member variables and methods defined in the parent class inherit from the quilt class, and can have different data types or behave differently. This allows the same variable and method to have different representations in the parent class and its subclasses. We use an example to understand what polymorphism, such as the "drawing" method of the "Geometry" class, also has a "drawing" method in its subclass "ellipse" and "triangle", but the "drawing" method functions differently.



The Graphics (geometry) class is the parent of the ellipse (oval) class and the triangle (triangle) class, and Ellipse and triangle override the OnDraw method.

@interface Graphics: NSObject {
}
-(void) onDraw;
@end
#import <Foundation / Foundation.h>
#import "Graphics.h"
@interface Ellipse: Graphics {
}
@end
#import <Foundation / Foundation.h>
#import "Ellipse.h"
@implementation Ellipse
-(void) onDraw {
NSLog (@ "Drawing an oval");
}
@end
#import <Foundation / Foundation.h>
#import "Graphics.h"
@interface Triangle: Graphics {
}
@end
#import <Foundation / Foundation.h>
#import "Triangle.h"
@implementation Triangle
-(void) onDraw {
NSLog (@ "Draw triangle");
}
@end
#import <Foundation / Foundation.h>
#import "Graphics.h"
#import "Ellipse.h"
#import "Triangle.h"
int main (int argc, const char * argv []) {
Graphics * graphics;
graphics = [[Ellipse alloc] init];
[graphics onDraw];
[graphics release];
graphics = [[Triangle alloc] init];
[graphics onDraw];
[graphics release];
return 0;
}
id is a generic data type, which can be used to store various types of objects. Using id means using "dynamic type".

int main (int argc, const char * argv []) {
id graphics;
graphics = [[Ellipse alloc] init];
[graphics onDraw];
[graphics release];
graphics = [[Triangle alloc] init];
[graphics onDraw];
[graphics release];
return 0;
}
Change Graphics * to id type, the result of program running has no effect. Due to the dynamic type, when the id is executed, the execution environment of Objective-C will find the original type represented by the id, so there is no so-called transformation at all. The id is not automatically converted to the parent class of Ellipse and Triangle, but during execution, the execution environment recognizes whether the type actually represented by id is Ellipse or Triangle. So in this example id has nothing to do with Graphics.

 

• Define two classes: "vector" and "scalar" classes.

• "Vector" is a quantity with direction and magnitude, such as "force" in physics. A scalar quantity is a quantity with no direction, such as "work" in physics.

 

@interface Vector: NSObject {
double vec1;
double vec2;
}
@property double vec1, vec2;
-(void) print;
-(void) setVec1: (double) v1 andVec2: (double) v2;
-(Vector *) add: (Vector *) v;
@end
#import "Vector.h"
@implementation Vector
@synthesize vec1, vec2;
-(void) setVec1: (double) v1 andVec2: (double) v2 {
vec1 = v1;
vec2 = v2;
}
-(Vector *) add: (Vector *) v {
Vector * result = [[Vector alloc] init];
[result setVec1: vec1 + [v vec1] andVec2: vec2 +
[v vec2]];
return result;
}
-(void) print {
NSLog (@ "% g,% g", vec1, vec2);
}
@end
@interface Scalar: NSObject {
double scal;
}
@property double scal;
-(void) print;
-(void) setScal: (double) sval;
-(Scalar *) add: (Scalar *) s;
@end
#import "Scalar.h"
@implementation Scalar
@synthesize scal;
-(void) print {
NSLog (@ "% g", scal);
}
-(void) setScal: (double) sval {
scal = sval;
}
-(Scalar *) add: (Scalar *) s {
Scalar * result = [[Scalar alloc] init];
[result setScal: scal + [s scal]];
return result;
}
@end
#import "Vector.h"
#import "Scalar.h"
int main (int argc, const char * argv []) {
Scalar * scA = [[Scalar alloc] init];
Scalar * scB = [[Scalar alloc] init];
Vector * vecA = [[Vector alloc] init];
Vector * vecB = [[Vector alloc] init];
id scAandB;
id vecAandB;
[scA setScal: 10.5];
[scB setScal: 13.1];
[vecA setVec1: 3.2 andVec2: 4.7];
[vecB setVec1: 32.2 andVec2: 47.7];
...
[vecA print];
NSLog (@ "+");
[vecB print];
NSLog (@ "=");
vecAandB = [vecA add: vecB];
[vecAandB print];
[scA print];
NSLog (@ "+");
[scB print];
NSLog (@ "=");
scAandB = [scA add: scB];
[scAandB print];
[scA release];
[scB release];
[scAandB release];
[vecA release];
[vecB release];
[vecAandB release];
return 0;
}
operation result:

3.2, 4.7
+
32.2, 47.7
=
35.4, 52.4
10.5
+
13.1
=
23.6

• The scAandB and vecAandB objects are both dynamic types and can be used with the "print" and "add" methods.

Although the id type can be any type of object, do not abuse it. If you can determine the data type of the object, use "static type". "Static type" checks for errors during the compilation phase, not during the execution phase. And the "static type" program is readable.

Objective-C language polymorphism

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.