Fourth Chapter Succession
inherited syntax format: @interface circle:nsobject. The mark behind the colon runes the class that needs to be inherited. Multiple inheritance is not supported in OC.
The first program with an inheritance
@interface shape:nsobject{
Shapecolor FillColor; Shaperect bounds;
}
-(void) Setfillcolor: (Shapecolor) FillColor;
-(void) SetBounds: (shapebounds) bounds;
-(void) draw;
@end//shape
@implementation Shape
-(void) Setfillcolor: (Shapecolor) C {
FillColor = C; }//setfillcolor
-(void) SetBounds: (shaperect) b {
bounds = b; }//setbounds
-(void) Draw {}//draw
@end//shape
@interface Circle:shape
@end//circle
@interface Rectangle:shape
@end//rectangle
Implementation part
@implementation Rectangle
-(void) Draw {
NSLog (@ "Drawing rect at (%d%d%d) in%@",
Bounds.x,bounds.y,bounds.width,bounds.height,colorname (FillColor));
}//draw
@end//rectangle
@implementation Circle-(void) Draw {
NSLog (@ "Drawing circle at (%d%d)%d] in%@",
Bounds.x,bounds.y,bounds.width,bounds.height,colorname (FillColor));
}//draw
@end//circle
Related terms
A superclass (superclass) is an inherited class. A super class such as Circle is shape, while shape's superclass is nsobject.
Subclasses (subclass) are classes that perform inheritance. Circle is a subclass of shape, while shape is a subclass of NSObject.
If you want to change the implementation of a method, you need to override (override) the inherited method. Because inheritance establishes a "is a" relationship between subclasses and superclass, the instance variable of NSObject is called Isa. That rectangle is a shape,circle is a shape. (Refer to the opinion of others on the Web: ISA is equivalent to the class of each object in Java, as we normally write, Xxx.getclass () or Xxx.class. ISA in OC points to its class object, think about it, we use reflection in Java not all need to get its class object! Like OC, the Class object ISA is also used to get the class information of the object at run time. This is in fact consistent with the class concept in Java. )
The ability to use more specific kinds of objects (rectangle and circle) instead of the generic type (shape) is known as polymorphism.
Each method call obtains a hidden argument named Self, which is a pointer to the object that receives the information. overriding refers to declaring a method in a class, but letting the implementation of the method be partially empty, and then each subclass can implement its own method, such as the draw method in the Shape class, which is an example.
OC provides a way to use super as a keyword so that you can override the implementation of the method and invoke the implementation in the superclass. For example:
@implementation Circle-(void) Setfillcolor: (Shapecolor) C {if (c==kredcolor) {c = Kgreencolor;} [Super SETFILLCOLOR:C]; } @end
Requests the superclass response information [super SETFILLCOLOR:C] and puts the color into the instance variable.
Super is a magical feature that OC provides, when sending a message to super, is actually requesting OC to send a message to the superclass class. If none is in the superclass, OC continues to look up to the upper level of the inheritance chain.
The fourth chapter ends here, except that the request for super-class response information is difficult to understand, and the rest is OK. From the beginning of the fifth chapter, the feeling is not good understanding, some knowledge points repeatedly read a few times or can not be very thorough understanding. Hey
OBJECTIVE-C Study Notes