// Bird的声明 @interface Bird : NSObject { @public int weight; } - (void)eat; @end // Bird的定义 @implementation Bird - (void)eat { NSLog(@"吃吃吃-体重:%d", weight); } @end // Dog的声明 @interface Dog : NSObject { @public int weight; } - (void)eat; @end // Dog的定义 @implementation Dog - (void)eat { NSLog(@"吃吃吃-体重:%d", weight); } @end
有相同的属性和行为,抽出一个父类Animal(先抽取weight属性,再抽取eat方法)
// Animal的声明 @interface Animal : NSObject { @public int weight; } - (void)eat; @end // Animal的定义 @implementation Animal - (void)eat { NSLog(@"吃吃吃-体重:%d", weight); } @end
子类在父类的基础上拓充属性和方法
// Bird的声明 @interface Bird : Animal { @public int height; } - (void)fly; @end // Bird的定义 @implementation Bird - (void)fly { NSLog(@"飞飞飞-高度:%d", height); } @end // Dog的声明 @interface Dog : Animal { @public int speed; } - (void)run; @end // Dog的定义 @implementation Dog - (void)run { NSLog(@"跑跑跑-高度:%d", speed); } @end
Person *p = [Student new]; p->age = 100; [p walk];
NSStirng *str = @“Hello”;
NSString *name = @”mj”;
NSLog(@“我的名字是%@”, name);
原文:http://www.cnblogs.com/chenziqiang/p/4930355.html