《OC基础教程》读书笔记3-继承
来源:互联网 发布:淘宝上的猛犸象牙牌子 编辑:程序博客网 时间:2024/06/05 20:41
OC中继承使用":",如:NSObject 表示继承自NSObject类。单继承,即只有一个父类。
父类Shape::
//Shape.h
@interface Shape:NSObject{
NSString* fillColor;
}
-(void)setFillColor:(NSString *)color;
-(void)draw;
@end
//Shape.m
@impementation Shape
-(void)setFillColor:(NSString *)color{
fillColor = color;
}
-(void)draw{
}
@end
子类Circle
//Circle.h
@interface Circle:Shape{
int radius;
}
-(void)setRadius:(int)r;
@end
//Circle.m
@implementation Circle
-(void)setRadius:(int)r{
radius = r;
}
//重写draw方法,不需要任何关键字
-(void)draw{
NSLog(@"draw circle, fillColor is %@ and radius is %d", fillColor, radius);
}
@end;
//另一个子类Rectangle
//Rectangle.h
@interface Rectangle:Shape
@end;
//Rectangle.m
@implementation Rectangle
-(void)draw{
NSLog(@"draw rectangle");
}
@end
实例化
Circle * c = [Circle new];
[c setFillColor:@"red"];
[c setRadius:10];
[c draw];
Rectangle *r = [Rectangle new];
[r draw];
- 《OC基础教程》读书笔记3-继承
- OC基础教程3-继承+复合
- 《OC基础教程》读书笔记4-复合
- 《OC基础教程》读书笔记1 第二章
- 《OC基础教程》读书笔记2-面向对象
- 《OC基础教程》读书笔记7-NSArray,NSMutableArray初识
- 《OC基础教程》读书笔记8-字典NSDictionary,NSMutableDictionary
- 《OC基础教程》读书笔记5-创建简单的用户界面
- 《OC基础教程》读书笔记6-NSString基础方法整理
- 《OC基础教程》读书笔记9-特性(set and get)
- Hibernete基础教程读书笔记(3-1)
- Hibernate基础教程读书笔记(3-2)
- Hibernate基础教程读书笔记(3-3)
- Objective-C基础教程读书笔记(3)
- 《Objective-C基础教程》读书笔记(3)
- 【《Objective-C基础教程 》笔记ch04】(五)OC中的继承inheritance机制
- OC基础教程8-类别
- OC基础教程9-协议
- CentOS安装ReviewBoard记录
- React开发实战pdf
- DHCP SERVER 不同网段运用
- Java语言Proxy 模式和 State 模式的区别
- springboot学习总结
- 《OC基础教程》读书笔记3-继承
- I/O复用之select服务器
- Query by Example (PrincipalSearcher)
- svn下载项目-上传项目
- Mercurial, 分支与合并
- unity
- Docker 最初的2小时(Docker从入门到入门)
- D19 Storm增强
- echarts仪表盘和模拟进度条