黑马程序员技术交流社区
标题: 更新关于OC中类的三大特性,一贴代码展示 [打印本页]
作者: tabor 时间: 2015-7-3 23:17
标题: 更新关于OC中类的三大特性,一贴代码展示
#import <Foundation/Foundation.h>
@interface Car: NSObject
{
int _wheels;
int _speed;
}
- (void)run;
- (void)carBeep;
- (void)setWheels: (int)wheels;
- (int)wheels;
@property int _speed;
@end
@implementation Car
- (void)setWheels: (int)wheels
{
_wheels = wheels;
}
- (int)wheels
{
return _wheels;
}
- (void)run
{
NSLog(@"车子跑起来了");
}
- (void)carBeep
{
NSLog(@"car 响了");
}
@end
@interface Aodi : Car
- (void)carBeep;
@end
@implementation Aodi
- (void)carBeep
{
NSLog(@"aodi 响了");
}
@end
@interface Baoma : Car
- (void)carBeep;
@end
@implementation Baoma
- (void)carBeep
{
NSLog(@"baoma 响了");
}
@end
void leep(Car *c)
{
[c carBeep];
}
int main()
{
@autoreleasepool
{
Baoma *baoma = [Baoma new];
leep(baoma);
Aodi *aodi = [Aodi new];
leep(aodi);
//Car *car = [Car new];
baoma.wheels = 5;
baoma._speed = 120;
int wnm = baoma.wheels;
NSLog(@"baoma 有%d个轮子",wnm);
//NSLog(@"%d个轮子的车子的速度是%d",car1->wheels,car1->speed);
}
return 0;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |