一、方法与成员变量
/*
人
类名:Person
属性(成员变量\实例变量):体重、年龄
行为(方法):走路、吃(先声明、在实现)
*/
#import <Foundation/Foundation.h>
/*
1.类的声明
* 成员变量:必须写在大括号{}里面
* 方法的声明
*/
@interface Person : NSObject
{
@public
// 成员变量...
int age;
double weight;
}
// 方法的声明
- (void)walk;
- (void)eat;
@end
// 2.类的实现
@implementation Person
// 实现@interface中声明的方法
- (void)walk
{
NSLog (@"%岁\%g公斤的人走了一段路", age, weight);
}
- (void)eat
{
NSLog (@"%岁\%g公斤的人在吃东西", age, weight);
}
@end
int main()
{
Person *p = [Person new];
p->age = 20;
p->weight = 40.0;
[p eat];
[p walk];
/*
Person *p2 = [Person new];
p2->age = 30;
p2->weight = 50.0;
p = p2; // p和p2共同指向同一个对象
p->age = 40;
[p2 walk];
*/
/*
Person *p = [Person new];
p->age = 20;
p->weight = 40.0;
Person *p2 = p; // p和p2共同指向同一个对象
p2->age = 30;
[p walk];
*/
/*
Person *p = [Person new];
p->age = 20;
Person *p2 = [Person new];
p2->weight = 50.0;
[p walk];
*/
/*
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];
*/
return 0;
}