zoukankan      html  css  js  c++  java
  • 类的扩展--类目和延展--ios

     person+money.h  这是类目类

    #import "Person.h"
    //这是扩展person类的接口类,独立一个文件
    @interface Person (Money)
    -(void) haveMoney;
    @end
    

     person+money.m 这是类目类

    #import "Person+Money.h"
    //这是扩展person类的实现类,独立一个文件
    @implementation Person (Money)
    -(void) haveMoney{
        NSLog(@"人人有钱");
    }
    @end
    

     person.h

    #import <Foundation/Foundation.h>
    //这是原始类
    @interface Person : NSObject
    @property(nonatomic,copy) NSString *name;
    @property(nonatomic,retain) NSNumber *age;
    -(void) originTest;
    @end
    
    //这是原始类的扩展类,直接定义与原始类同一文件中
    @interface Person (Creat)
    +(id) personWithName:(NSString *) name;
    @end
    
    //这是原始类的扩展类,直接定义与原始类同一文件中
    @interface Person (Test)
    -(void) test;
    @end
    

     person.m

    #import "Person.h"
    //这是person类的实现类
    @implementation Person:NSObject
    
    -(id) init{
        self=[super init];
        NSLog(@"调用类自定义的init 方法");
        return self;
    }
    
    +(id) personWithName:(NSString *) name{
        Person *person=[[Person alloc] init];
        person.name=name;
        return person;
    }
    //这是person类的实现类,直接定义与原始类同一文件中
    -(void) test{
        NSLog(@"这是类目定义的方法");
    }
    
    -(void) originTest{
      NSLog(@"这是原始类的方法");
    }
    -(void) haveMoney{
      NSLog(@"人人有钱");
    }
    @end
    

     person_family.h  这是延展的类

    #import "Person.h"
    
    @interface Person ()
    -(void) haveFamily;
    @end
    

    main.m

      //类目扩展学习

            Person *person=[Person personWithName:@"caictou"];

            //这是原始类定义的方法

            [person originTest];

            //这是同一个文件的扩展类定义的方法

            [person test];

            //这是独立一个文件的扩展类定义的方法

            [person haveMoney];

            //这是对立一个文件的延展类定义的方法

            [person haveFamily];

  • 相关阅读:
    MongoDB+Lucence.net
    hubble+sqlserver
    C# 设计模式 1 接口模式 1.1 适配器模式 IT
    SQLServer2005 中 XML类型方法中 XQuery中变量的参数化匆忙整理 IT
    DoNET 类库设计准则01 名称规则 IT
    GMRES在matlab中的描述
    矩阵良态与病态
    调试vc++的一点感悟
    基于GramSchmidt正交法的广义极小残量法(GMRES)
    VC6 vs2003 vs2005 使用技巧(转)
  • 原文地址:https://www.cnblogs.com/clarence/p/3923530.html
Copyright © 2011-2022 走看看