多态,子类指针可以赋值给父类,不同对象对同一消息的不同响应.
person.h
- @interface Person : NSObject
- @property (nonatomic,assign) NSString *name;
- -(void) show;
- @end
复制代码 person.m
- @implementation Person
- -(void)show{
- NSLog(@"我是%@",self.name);
- }
- @end
复制代码
@interface Teacher : Person @interface Student : Person - #import <Foundation/Foundation.h>
- #import "Person.h"
- #import "Teacher.h"
- #import "Student.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
-
- Student *stu=[Student new];
- stu.name=@"小明";
- [stu show];
- Teacher *tec=[Teacher new];
- tec.name=@"王五";
- [tec show];
- }
- return 0;
- }
复制代码输出: 2014-04-07 09:45:55.800 多态[1669:303] 我是小明 2014-04-07 09:45:55.810 多态[1669:303] 我是王五
|