OC 分类
来源:互联网 发布:计算一组数据的标准差 编辑:程序博客网 时间:2024/05/18 01:40
分类是在不改变类代码的前提下,扩充类方法
//// Student.h// 分类//// Created by LiuWei on 15/4/15.// Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject{ int _age; // @protected 成员变量}@property int age;@property NSString *name; // 生成 @private成员变量- (void)test;@end
//// Student.m// 分类//// Created by LiuWei on 15/4/15.// Copyright (c) 2015年 LiuWei. All rights reserved.//#import "Student.h"@implementation Student- (void)test{ NSLog(@"Student --- test!");}@end
//// Student+add.h// 分类//// Created by LiuWei on 15/4/15.// Copyright (c) 2015年 LiuWei. All rights reserved.//#import "Student.h"// 分类用于扩充原来类的方法 包括类方法和对象方法@interface Student (add)- (void)showInfo;+ (void)classInfo;// 如果分类中有和被扩充类同名的方法, 则优先调用分类中的方法. 原来方法会被覆盖- (void)test;@end
//// Student+add.m// 分类//// Created by LiuWei on 15/4/15.// Copyright (c) 2015年 LiuWei. All rights reserved.//#import "Student+add.h"@implementation Student (add)- (void)showInfo{ _age = 20; NSLog(@"age = %i", _age); // _name = @"LiLei"; 分类中不可以直接访问 @private 成员变量 self.name = @"LiLei"; NSLog(@"name = %@", self.name);}+ (void)classInfo{ NSLog(@"我是分类的类方法");}- (void)test{ NSLog(@"分类中的test方法运行了");}@end
//// main.m// 分类//// Created by LiuWei on 15/4/15.// Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>#import "Student+add.h"int main(int argc, const char * argv[]) { Student *s = [[Student alloc]init]; [s test]; // 调用分类的test方法 [Student classInfo]; // 调用分类扩充的类方法classInfo [s showInfo]; // 通过类方法访问成员变量 return 0;}
0 0
- OC 分类
- OC分类
- OC 分类
- oc----分类
- OC 分类
- OC之【分类】
- OC与Category 分类
- oc协议,延展,分类
- oc 分类与协议
- 黑马程序员--OC--分类
- OC分类和协议
- OC语言--分类(category)
- OC之分类
- OC分类浅谈
- OC中的分类Category
- oc005---oc分类 categroy
- oc数据类型分类
- OC内存管理分类
- 单链表基本操作
- LeetCode—Merge Two Sorted Lists融合两个有序单链表
- Android html 超链接 启动app应用
- 网站常用字体
- Handler 引起的内存泄露
- OC 分类
- shell脚本中的日期处理
- flask 中的蓝图 blueprint 的理解
- eXtremeDB connection pool
- 4、spss做多元线性回归
- java中switch() case用法和效率
- 链表各类操作详解
- C++面试笔试经典题集
- HDU 5202 Rikka with string (水DFS)