• 手动实现类的属性


    oc类中实现属性有两种方法,一种是使用OC自定义的格式来实现类的属性,另一种是手动实现类的属性,本篇文章就是章节如何通过自定义的方式实现类的属性效果:

    定义一个student类,具体的代码如下所示:

    //student.h

    #import <Foundation/Foundation.h>

     

    @interface student : NSObject 

    {

    int Age;

    NSString *Name;

    }

     

    -(void)setAge:(int)_age;

    -(void)setName:(NSString *)_name;

    -(int)Age;

    -(NSString *)Name;

     

    @end

    //student.m

    #import "student.h"

    #import <Foundation/Foundation.h>

     

    @implementation student

     

    //定义Age成员变量的读取器

    //定义Name成员变量的读取器

    -(void)setAge:(int)_age

    {

    Age = _age;

    }

    -(void)setName:(NSString *)_name

    {

    Name = _name;

    }

     

    -(int)Age

    {

    return Age;

    }

     

    -(NSString *)Name

    {

    return Name;

    }

    @end

    调用的代码:

    #import <Foundation/Foundation.h>

    #import "student.h"

     

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

    {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    /////////////////////

    student *s = [[student alloc] init];

    [s setName:@"xingchen"];

    [s setAge:100];

    NSLog([s Name]);

    /////////////////////

    [pool drain];

    return 0;

    }

    THE END !

  • 相关阅读:
    [原]Eclipse 安装SVN、Maven插件(补充)
    [原]几个云笔记的简单比较
    [原]Unity3d中奇怪的编译错误
    [原]unity中WWW isDone方法只能在主线程中调用
    C语言。格式化符号
    Unity iOS Guideline 1.3
    AR增强现实 之Metaio For Unity 开发 之HelloWorld
    Unity 4.6 bate 20 or 4.5.5 +vuforia3.0.9 发布到真机错误 解决
    服务器端Ajax异步分页类,基本通用分页类
    从数据库中读取数据并写入到Excle电子表格之2
  • 原文地址:https://www.cnblogs.com/xingchen/p/2098712.html
Copyright © 2020-2023  润新知