方法和函数的区别
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int wheels;
}
- (void)run;
- (void)test;
@end
@implementation Car
//void run()
- (void)run
{
NSLog(@"%i个轮子的车跑起来了", wheels);
}
- (void)test
{
NSLog(@"测试一下车子:%i", wheels);
}
@end
//- (void)haha 明显应该是全局函数
void haha()
{
NSLog(@"调用了haha");
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Car *c = [Car new];
[c run];
[c test];
//test();
haha(); }
return 0;
}
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int wheels;
}
- (void)run;
- (void)test;
@end
@implementation Car
//void run()
- (void)run
{
NSLog(@"%i个轮子的车跑起来了", wheels);
}
- (void)test
{
NSLog(@"测试一下车子:%i", wheels);
}
@end
//- (void)haha 明显应该是全局函数
void haha()
{
NSLog(@"调用了haha");
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Car *c = [Car new];
[c run];
[c test];
//test();
haha(); }
return 0;
}
4.分析
#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
@public
int age;
double height;
}
- (void)print;
@end
void test1(int newAge, double newHeight);
void test2(Person *newP);
void test3(Person *newP);
void test4(Person *newP);
@implementation Person
- (void)print
{
NSLog(@"年龄=%d,身高=%f", age, height);
}
@end
void test1(int newAge, double newHeight)
{
newAge = 10;
newHeight = 1.6;
}
void test2(Person *newP)
{
newP->age = 20;
newP->height = 1.7;
}
void test3(Person *newP)
{
Person *p2 = [Person new];
p2->age = 40;
p2->height = 1.8;
newP = p2;
newP->age = 30;
}
void test4(Person *newP)
{
Person *p2 = newP;
p2->age = 50;
p2->height = 1.9;
newP->age = 60;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Person *p = [Person new];
p->age = 10;
p->height = 1.5f;
test1(p->age, p->height);
[p print];//10,1.5
test2(p);
[p print];//20,1.7
test3(p);
[p print];//20 1.7
test4(p);
[p print];//60,1.9
}
return 0;
}
匿名对象
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
@public
int age;
double height;
}
- (void)print;
@end
@implementation Person
- (void)print
{
NSLog(@"年龄=%d,身高=%f", age, height);
}
@end
int main()
{
[Person new]->age = 10;
[Person new]->height = 1.8;
[[Person new] print];//0,0?
return 0;
}
编程题
/*要求:
* 类名、属性名、属性类型、方法名、方法参数、方法返回值自拟
* 自己写main函数测试设计是否合理
1.设计一个”狗“类
1> 属性
* 颜色
* 速度(单位是m/s)
* 性别
* 体重(单位是kg)
2> 行为
* 吃:每吃一次,体重增加0.5kg,输出吃完后的体重
* 吠(叫):输出所有的属性
* 跑:每吃一次,体重减少0.5kg,输出速度和跑完后的体重
* 比较颜色:跟别的狗比较颜色,如果一样,返回YES,不一样,返回NO
* 比较速度:跟别的狗比较速度,返回速度差(自己的速度 - 其他狗的速度)*/
dog.h
#ifndef dog_h
#define dog_h
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
@public
NSString *color;
double speed;
NSString *sex;
double weight;
}
- (void)Eat;
- (void)Bark;
- (void)Run;
-(Boolean)CmpColor:(Dog*) pDog;
-(double)CmpSpeed:(Dog*) pDog;
@end
#endif
dog.m
#import <Foundation/Foundation.h>
#import "dog.h"
@implementation Dog
- (void)Eat
{
weight += 0.5;
NSLog(@"after eat weight:%g",weight);
}
- (void)Bark
{
NSLog(@"颜色:%@",color);
NSLog(@"速度:%g m/s",speed);
NSLog(@"性别:%@",sex);
NSLog(@"体重:%g kg",weight);
}
- (void)Run
{
weight -= 0.5;
NSLog(@"速度:%g m/s",speed);
NSLog(@"体重:%g kg",weight);
}
-(Boolean)CmpColor:(Dog*) pDog
{
if ([color isEqualToString:pDog->color])
{
NSLog(@"2条狗颜色相同");
return true;
}
NSLog(@"2条狗颜色不同");
return false;
}
-(double)CmpSpeed:(Dog*) pDog
{
return speed - pDog->speed;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Dog *dog_st = [Dog new];
dog_st->color = @"红色";
dog_st->speed = 5.0f;
dog_st->sex = @"雌性";
dog_st->weight = 20.0f;
Dog *dog_nd = [Dog new];
dog_nd->color = @"红色";
dog_nd->speed = 6.0f;
dog_nd->sex = @"雌性";
dog_nd->weight = 21.0f;
[dog_st Eat];
[dog_st Bark];
[dog_st Run];
[dog_st CmpColor:dog_nd];
double spCmp = [dog_st CmpSpeed:dog_nd];
NSLog(@"dog_st和dog_nd的速度差是:%g",spCmp);
}
return 0;
}