zoukankan      html  css  js  c++  java
  • oc59--匿名分类

    //
    //  main.m
    //  匿名分类(延展)
    //  可以为某个类扩展私有的成员变量和方法,写在.m文件中,
    //  分类不可以扩展属性,分类有名字,匿名分类没有名字。
    
    #import <Foundation/Foundation.h>
    #import "Person.h"
    
    int main(int argc, const char * argv[]) {
    
        Person *p = [Person new];
        
        p->_age;  //报错
        [p say];  //报错
        
        return 0;
    }
    //
    //  Person.h
    
    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    
    //{
    //    int _age;
    //}
    
    //- (void)eat;
    //- (void)say;
    
    @end
    //
    //  Person.m
    
    #import "Person.h"
    
    @interface Person ()   //没有名字,所以叫匿名分类,不需要实现,
    {
        int _age;  //私有的属性
    }
    
    - (void)say;   //私有的方法
    
    @end
    
    @implementation Person
    
    //{
    //    int _age;  //私有的属性
    //}
    
    -(void)eat    //没有声明只有实现,方法是私有的,
    {
        NSLog(@"%s", __func__);
    }
    
    - (void)say   //没有声明只有实现,方法是私有的,
    {
        NSLog(@"age = %i", _age);
    }
    @end
  • 相关阅读:
    4.变量以及类型
    3.注释
    2.第一个python程序
    1.认识Python
    DB安装
    DB2<RedHed Linux> 创建数据库
    win 7设置主机域名
    FTP 错误1
    FTP 其他设置
    VM浏览器不能访问
  • 原文地址:https://www.cnblogs.com/yaowen/p/7436182.html
Copyright © 2011-2022 走看看