oc语法之声明类和对象

//
//  main.m
//  study2023
//
//  Created by zhifei  zhu on 2023/7/30.
//

#import <Foundation/Foundation.h>
//声明类 @interface开头 @end结尾
@interface Person : NSObject
{
    //声明属性为public,这样对象就能访问
    @public
    NSString *_name;
    int _age;
    float _height;
}
//无参方法
- (void)run;
//无返回值,有参
-(void)eat:(NSString *)foodname;
//有返回值,有参
-(int)sum:(int)num1:(int)num2;
//有返回值,有参,提供可读性写法
-(int)sumWith:(int)num1 and:(int)num2;
@end

//实现类 @implementation开头 @end结尾
@implementation Person
//实现方法
- (void) run
{
    NSLog(@"i'm running!");
}
-(void)eat:(NSString *)foodname
{
    NSLog(@"%@,is delicious",foodname);
}
-(Boolean)jump:(int) account
{
    if(account>=1000){
        NSLog(@"完成任务!");
        return true;
       
    }else
    {
        NSLog(@"未完成任务!");
        return false;
    }
}
-(int)sum:(int)num1:(int)num2
{
    return num1+num2;
}
-(int)sumWith:(int)num1 and:(int)num2
{
    return num1+num2;
}
@end
//声明方法
void test();
//实现方法
//        常用的一些占位符:
//        %@:字符串占位符
//        %d:整型,BOOL,Boolean也用这个
//        %ld:长整型
//        %f:浮点型
//        %c:char类型
//        %%:%的占位符
void test(){
    NSLog(@"大家好,好好学习,天天向上!");
    BOOL b=YES;
    NSLog(@"b=%d",b);
}
//主方法,oc运行开始的地方
int main(int argc, const char * argv[]) {
    float f=12.18f;
    NSString *str=@"hello,china!";
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        NSLog(@"float value= %f",f);
        NSLog(@"str value= %@",str);
        NSLog(str);
        //test();
        Person *p=[Person new];
//        p->_name=@"figo";
//        p->_age=18;
//        p->_height=1.75f;
        (*p)._name=@"rose";
        (*p)._age=18;
        (*p)._height=1.65f;
        NSLog(p->_name);
        NSLog(@"%d",p->_age);
        NSLog(@"%f",p->_height);
        //调用方法
        [p run];
        [p eat:@"鸡蛋饼"];
        Boolean b=[p jump:2000];
        NSLog(@"%@",b?@"YES":@"NO");
        NSLog(@"%d",b);
        int sum=[p sum:1 :2];
        NSLog(@"%d",sum);
        NSLog(@"%d",[p sumWith:2 and :3]);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值