黑马程序员技术交流社区
标题: OC方法声明练习问题? [打印本页]
作者: holfioo 时间: 2015-5-18 11:16
标题: OC方法声明练习问题?
本帖最后由 holfioo 于 2015-5-18 12:32 编辑
视频练习中这个题目。
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int speed;
}
- (void) compareSpeedWithOther: (Car *)other ;
@end
@implementation Car
- (void) compareSpeedWithOther: (Car *)other
{
// speed;
// other->speed;
//返回当前这辆车和other这辆车的速度差距
int com = speed - other ->speed;
NSLog(@"第一辆车与第二辆车速度差是%i",com);
}
@end
int main()
{
Car *c1 = [Car new];
c1->speed = 200;
Car *c2 = [Car new];
c2->speed = 280;
[c1 compareSpeedWithOther:c2];
//NSLog(@"这两辆车的速度差是%i",com);
return 0;
}
我改写了一下。。无返回值的声明方法声明。
现在不明白这个声明。
- (void) compareSpeedWithOther: (Car *)other
括号()中不是只能参数类型吗?
Car * 作为参数是什么意思?
表达式: speed - other -> speed; 也没听懂。。
求解答- - 迷糊状态中。
作者: mingjixiaohui 时间: 2015-5-18 16:25
- (void) compareSpeedWithOther: (Car *)other 是compareSpeedWithOther方法的声明,冒号后边是传入的参数,括号是要传入的参数类型(Car *),要传入Car的指针类型,other -> speed 中的“->”是指向other的成员变量的意思.
作者: holfioo 时间: 2015-5-18 16:43
嗯。刚研究了一下。。现在明白了。。(指针) 。。就是传了个指针类型。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |