- // [代码说明:类的多态体现及用途]
- // 说明:下方为伪代码,其中police是一个继承自person的类,thief 也是一个继承自person的类
- @interface Person :NSObjec{ //---->父类
- NSStrint *_name;
- }
- -(void)setName:(NSString *)name;
- -(void)name;
- @end
- @implementation
- -(void)setName:(NSString *)name{
- _name = name;
- }
- -(NSString*)name{
- return _name;
- }
- @end
- @interface Police :person{} //子类police的声明
- @end
- .............实现省略
- @interface Thief :person{} //子类thief的声明
- @end
- .............实现省略
- void methdDisplay(Person *per)
- {
- NSlog(@"methdDISplay is %@",per.name);
- }
- int main()
- {
- Police *policeMan = [Police new];
- policeMan.name = @"police SIR";
- methdDisplay(policeMan); //此处打印的是:methdDISplay is police SIR
- Thief *thiefA = [Thief new];
- thiefA.name = @"xiaotou A";
- methdDisplay(thiefA); //此处打印的是:methdDISplay is xiaotou A
- return 0;
- }
复制代码 |
|