zoukankan      html  css  js  c++  java
  • OC中结构体作为对象属性

    在OC中结构体有时候也作为对象的属性

    类的定义

    #import <Foundation/Foundation.h>
    
    typedef struct{
        int year;
        int month;
        int day;
    
    } Date;
    @interface Student : NSObject
    {
        @public
        NSString *_name;
        Date _birthday;
    }
    -(void) say;
    @end

    类方法

    #import "Student.h"
    
    @implementation Student
    -(void) say{
        NSLog(@"name=%@;year=%d,month=%d,day=%d",_name,_birthday.year,_birthday.month,_birthday.day);
    }
    @end

    对象的实例化及方法的实现

    #import <Foundation/Foundation.h>
    #import "Student.h"
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Student *stu=[[Student alloc] init];
            stu->_name=@"喵星人";
            //方法1
        //    stu->_birthday=(Date){1995,2,15};
            //方法2
    //        stu->_birthday.year=1995;
    //        stu->_birthday.month=2;
    //        stu->_birthday.day=25;
            //方法3
            Date date={1995,2,25};
            stu->_birthday=date;
            [stu say];
        }
        return 0;
    }

  • 相关阅读:
    Nginx配置文件的路径
    有关Tomcat 8.5版本文件上传后无权限访问的问题
    常见HTTP状态码列表
    服务器BMC(带外)
    CDN问题
    PECE
    linux系统概述
    干货--整蛊你的舍友
    arp请求与回复
    huawei oceanstor
  • 原文地址:https://www.cnblogs.com/qianLL/p/5100140.html
Copyright © 2011-2022 走看看