oc基础学习笔记---结构体及相关

11. 定义并使用结构体类型

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        struct Point{
            int a;
            int b;
        }p3={4,6},p4={99,22};
        
        struct Rect{
            int width;
            int heigt;
        } r3,r4;
        
        struct Point p1;
        struct Point p2;
        
        struct Rect r1={55,66};
        struct Rect r2;

        p1.a=5;
        NSLog(@"%i",p1.a);
        NSLog(@"%i",p3.a);
        typedef struct Point PIONT ;
        PIONT p5;
        p5.b=233;
        NSLog(@"%i",p5.b);
        NSLog(@"%i",r1.heigt);
        
        struct Point points[]={
            {22,33},{44,55},{66,77}
        };
        typedef struct Rect RECT;
        RECT rects[]={
            {111,222},{333,444},{666,777}
        };
        
        for (int i=0; i<sizeof(rects)/sizeof(rects[0]); i++) {
            NSLog(@"width=%i,height=%i",rects[i].width,rects[i].heigt);
        }

    }
    return 0;
}

12. 定义并使用指针

&取地址
*从地址中取值

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int i1=5,i2=99;
        int *p1, *p2;
        NSLog(@"%p",&p1);
        p1=&i1;
        p2=&i2;
        NSLog(@"%p",&p1);
        NSLog(@"after %i",*p1);
        *p1=10;
        NSLog(@"%i",i1);
        NSLog(@"after %i",*p1);
    }
    return 0;
}

13. 对象和指针的关系

Person *p1;
p1为Person类对象的指针

p1=[Person alloc];
为p1分配内存。

14. 通过指针的运算取数组元素的值

i

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

    @autoreleasepool {
        int myarray[]={11,22,33,44,55};
        for (int i=0,length=sizeof(myarray)/sizeof(myarray[0]); i<length;i++) {
            NSLog(@"%i",*(myarray+i));
        }
    }
    return 0;
}

15. 声明、实现及使用类及实例

1创建声明文件Classic.h

@interface Classic:NSObject
{
    NSString *_name;
    int _age;
}

-(void)setName:(NSString *)name andAge:(int)age;
-(void)say:(NSString *)content;
-(NSString *)info;
+(void)foo;

@end

2创建实现文件Classic.h

#import "Classic.h"
@implementation Classic:NSObject
{
    int testValue;
}
-(void)setName:(NSString *)name andAge:(int)age{
    _name=name;
    _age=age;
}
-(void)say:(NSString *)content{
    NSLog(@"here is the %@",content);
}
-(NSString *)info{
    [self test];
    return [NSString stringWithFormat:@"here is a person with name is %@ and age is %i",_name,_age];
}
-(void)test{
    NSLog(@"here is a method just effect at inner");
}
+(void)foo{
    NSLog(@"this is a classic method");
}
@end

3主函数

#import "Classic.h"

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

    @autoreleasepool {
        Classic *adclass=[[Classic alloc]init];
        [adclass say:@"hello this is a say method"];
        [adclass setName:@"zhangxiaosan" andAge:30];
        NSString *strings=[adclass info];
        NSLog(@"here is the string %@",strings);
        
        //[adclass test];不被显示,被隐藏了
        [Classic foo];
        Classic * chclass=adclass;
        [chclass say:@"hello this is a say method"];

    }
    return 0;
}

结果:
here is the hello this is a say method
here is a method just effect at inner
here is the string here is a person with name is zhangxiaosan and age is 30
this is a classic method
here is the hello this is a say method

16. 使用继承

1父文件basic.h

@interface Basic : NSObject
{
    int x;
}
-(void)setX;

@end

父文件实现文件basic.m

#import "Basic.h"
@implementation Basic
-(void)setX{
    x=10;
}
@end

2、子文件jichen.h

#import "Basic.h"

NS_ASSUME_NONNULL_BEGIN
@interface Jichen : Basic

-(void)setX;
-(void)printX;
@end

子文件jichen.m

#import "Jichen.h"
@implementation Jichen
-(void)setX{
    x=100;
}
-(void)printX{
    NSLog(@"the value of x is:%i",x);
}
@end

3、主函数

#import "Basic.h"
#import "Jichen.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
         Jichen * jichen=[[Jichen alloc]init];
        [jichen setX];
        [jichen printX];
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值