OC中的类及实现方法

来源:互联网 发布:电信宽带网络线路故障 编辑:程序博客网 时间:2024/05/01 20:25

多个同种事物的抽象称为类;

类类型的属性称为对象;

#import <Foundation/Foundation.h> //声明一个类

@interface NEat : NSObject

{

    @public

   NSString *_name;

   int _age;

   float _weight;

}

-(void)eat:(NSString*)food;

-(void)walk:(int)walking;

@end


#import "NEat.h" //实现类的方法

@implementation NEat

-(void)eat:(NSString*)food{

   _weight+=0.6f;

    NSLog(@"%@,体重为%.1f公斤",food,_weight);

}

-(void)walk:(int)walking{

   _weight=_weight-walking/100*0.2;

    NSLog(@"散步%d,体重为%.1f公斤",walking,_weight);

}

@end


#import <Foundation/Foundation.h>

#import "NEat.h" //调用类

int main(int argc,const char * argv[]) {

    @autoreleasepool {

       NEat *tu=[NEatalloc];

        tu->_weight=100;

        [tueat:@"面包"];

        [tuwalk:200];

    }

   return 0;

}



0 0
原创粉丝点击