//创建一个猪类,猪的属性是重量,行为是吃 要每吃一次 长一斤
#import<Foundation/Foundation.h>
//定义类
@interface Pig : NSObject{
@public
float weight;
}
//方法声明
-(void) Eat;
@end
//实现方法
@implementation Pig
-(void) Eat{
weight+=1;
NSLog(@"猪的当前重量为: %.2f",weight);
}
@end
int main(){
//定义对象并赋值
Pig *p=[Pig new];
p->weight=238.01f;
for (int i=0; i<5; i++) {
[p Eat];
}
return 0;
} |