main:
1 #import <Foundation/Foundation.h> 2 #import "Location.h" 3 #import "Rectangle.h" 4 5 int main(int argc, const char * argv[]) { 6 @autoreleasepool { 7 Location *loc=[[Location alloc]init]; 8 [loc setX:12]; //因为使用了@propery和@synthesize,所以可以用setX给x赋值 9 [loc setY:15]; 10 [loc description]; 11 Rectangle *rec=[[Rectangle alloc]init]; 12 [rec setX:190]; 13 [rec setY:210]; 14 [rec setHeight:120]; 15 [rec setWidth:150]; 16 [rec description]; 17 } 18 return 0; 19 }
Location.h:
1 #import <Foundation/Foundation.h> 2 3 @interface Location : NSObject 4 @property int x; 5 @property int y; 6 -(void)description; 7 @end
Location.m:
1 #import "Location.h" 2 3 @implementation Location 4 -(void)description{ 5 NSLog(@"x=%d,y=%d",x,y); 6 } 7 @synthesize x,y; 8 @end
Rectangle.h:
1 #import "Location.h" 2 3 @interface Rectangle : Location 4 @property int height; 5 @property int width; 6 -(void)description; 7 @end
Rectangle.m:
1 #import "Rectangle.h" 2 3 @implementation Rectangle 4 @synthesize height,width; 5 -(void)description{ 6 NSLog(@"x=%d,y=%d,height=%d,width=%d",super.x,super.y,self.height,self.width); 7 //super.x是父类中的x,self.height是本类中的height,“self.”可以省略 8 } 9 @end
原文:http://www.cnblogs.com/wangyang1987/p/5107387.html