/*
人
类名:Person
属性(成员变量\实例变量):体重,年龄
行为(方法):走路
*/
/*
1.类的声明:成员变量,方法的声明
*/
#import <Foundation/Foundation.h>
@interface Person:NSObject
{
@public
//int age=5 ;不允许在这里初始化
//static int wheels ; 不能随便将成员变量当成c语言中的变量来使用
int age;
double weight;
}
-(void) walk;
@end
//2.类的实现
@implementation Person
//实现@interface中声明的方法
-(void) walk
{
NSLog(@"%d岁,%f公斤的人走了一段路",age,weight);
}
@end
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Person *p=[Person new];
p->age=20;
p->weight=50.0;
[p walk];
Person *p2=[Person new];
p2->age=30;
p2->weight=60.0;
[p2 walk];
[pool drain];
return 0;
}
/*
方法和函数的区别:
方法:
1.对象方法都是以减号-开头
2.对象方法的声明必须写在@interface和@end之间
对象方法的实现必须写在@implementation和@end之间
3.对象方法只能由对象调用
4.对象方法归类/对象所有
函数:
1.函数能写在文件任意位置(@interface和@end之间),函数归文件所有
2.函数调用不依赖于对象
3.函数内部不能直接通过成员变量名访问某个对象的成员变量
*/