objective-C中的扩展方法与partial class
|
1
2
3
4
5
6
7
|
public static class Utils{ public static void PrintToConsole(this string strSrc) { Console.WriteLine(strSrc); } } |
|
1
2
3
4
5
6
7
|
class MainClass{ public static void Mainstring[] { "Hello.PrintToConsole(); }} |
- #import <Foundation/Foundation.h>
- @interface NSString(ExtNSString)
- -(void) PrintToConSole;
- @end
- #import "StringUtils.h"
- @implementation NSString(ExtNSString)
- -(void) PrintToConSole
- {
- NSLog(@"%@",self);
- }
- @end
- #import <Foundation/Foundation.h>
- #import "StringUtils.h"
- int main (int argc, const charchar * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- NSString* str = @"Hello World!";
- [str PrintToConSole];
- [pool drain];
- return 0;
- }
一个好的习惯是为全部扩展方法(包含类名),都加一个特殊的前缀或后缀。以避免重名。
- #import <Foundation/Foundation.h>
- @interface BLL : NSObject {
- NSString* connStr;
- }
- -(void) setConnString:(NSString*) connString;
- -(NSString*) connString;
- @end
- #import "BLL.h"
- @implementation BLL
- -(void) setConnString:(NSString *)connString
- {
- connStr = connString;
- }
- -(NSString*) connString
- {
- return connStr;
- }
- -(void) dealloc
- {
- [connStr release];
- [super dealloc];
- }
- @end
- #import <Foundation/Foundation.h>
- #import "BLL.h"
- @interface BLL(Product)
- -(void) addProduct: (NSString* )productName productNo:(NSString*)proNo;
- -(void) deleteProduct:(NSString*) productNo;
- @end
- #import "Product.h"
- #import "BLL.h"
- @implementation BLL(Product)
- -(void) addProduct: (NSString* )productName productNo:(NSString*)proNo
- {
- NSLog(@"connString=%@",connStr);//输出Bll.h中定义的成员connStr
- NSLog(@"addProduct success! productName:%@,productNo:%@",productName,proNo);
- }
- -(void) deleteProduct:(NSString*) productNo
- {
- NSLog(@"connString=%@",[self connString]);//也能够用属性来訪问
- NSLog(@"deleteProduct success! productNo:%@",productNo);
- }
- @end
3、定义Order.h继续扩展BLL类
- #import <Foundation/Foundation.h>
- #import "BLL.h"
- @interface BLL(Order)
- -(void) createOrder:(NSString*) productNo quantity:(int) amount;
- @end
Order.m
- #import "Order.h"
- @implementation BLL(Order)
- -(void) createOrder:(NSString*) productNo quantity:(int) amount
- {
- NSLog(@"thank you for order our product. productNo:%@,quantity:%d",productNo,amount);
- }
- @end
因为Product类与Order类都是扩展自BLL类。所以这三个类在逻辑上都是同一个类BLL,最后来看看怎样使用:
- #import <Foundation/Foundation.h>
- #import "BLL.h"
- #import "Product.h"
- #import "Order.h"
- int main (int argc, const charchar * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- BLL *bll = [[BLL alloc] init];
- bll.connString = @"I am connection string.";
- [bll addProduct:@"iphone4" productNo:@"0001"];//调用Product.h中定义的方法
- [bll createOrder:@"0001" quantity:5]; //调用Order.h中定义的方法
- [bll deleteProduct:@"0001"];
- [bll release];
- [pool drain];
- return 0;
- }
执行结果:
2011-02-26 22:29:30.369 Demo[1292:a0f] connString=I am connection string.
2011-02-26 22:29:30.376 Demo[1292:a0f] addProduct success! productName:iphone4,productNo:0001
2011-02-26 22:29:30.378 Demo[1292:a0f] thank you for order our product. productNo:0001,quantity:5
2011-02-26 22:29:30.379 Demo[1292:a0f] connString=I am connection string.
2011-02-26 22:29:30.380 Demo[1292:a0f] deleteProduct success! productNo:0001
皆大欢喜。非常多语言和技术真是“一门通。处处通”,或许:c#中的"扩展方法"与"部分类"的设计灵感正是来自objective-C。
objective-C中的扩展方法与partial class的更多相关文章
- C#3.0中的扩展方法
在实际应用中,开发者完成代码的编译后,除非重新编译更改后的代码,否则开发者很难在原有代码中添加新的功能. 在C#3.0中,提供了一个扩展方法的新特性,可以使得开发者在编译后的程序集里边添加相关的方法, ...
- 记录C#中的扩展方法
C#中的扩展方法. 系统自带的类型,我们无法去修改: 修改源代码需要较大的精力,而且可能会带来错误: 我们只是需要一个或者较少的几个方法,修改源代码费时费力: 被扩展的类是sealed的,不能被继承: ...
- C#编程(六十一)------------LINQ中的扩展方法
原文链接: http://blog.csdn.net/shanyongxu/article/details/47208401 LINQ中的扩展方法 LINQ中where扩展方法,要想使用,必须导入us ...
- C#中的扩展方法(向已有类添加方法,但无需创建新的派生类型)
C#中的扩展方法 扩展方法使你能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样 ...
- C#中的扩展方法详解
“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.”这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些类 ...
- C#中的扩展方法
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 以上是msdn官网对扩展方 ...
- Enum扩展及MVC中DropDownListFor扩展方法的使用
public enum SearchState { /// <summary> /// 全部 /// </summary> [Description("全部" ...
- 19.C#逐一介绍IEnumerable和IEnumerable<T>中的扩展方法(10.3-10.5)
今天没有太多的言语,只有代码,扩展方法多得太多,不能一一列完,书中一些,看多了也就会使用了. //Enumerable.Range 返回起始到结束范围,是一个Enumrable<int>类 ...
- MVC 中使用扩展方法
扩展方法(Extension Method)是给那些不是你拥有.因而不能直接修改的类添加方法的一种方便的办法. 一.使用扩展方法 1.定义一个购物车的类-ShoppingCart using Sys ...
随机推荐
- aliyun
阿里云启动不了网站 1 将网站的目录属性-安全中加入IUSER_计算机名字的访问权限 和 加入NER SERVICE的访问权限 2 IIS打开网站属性--目录--执行权限改为顺脚本 3 ...
- Python_Tips[6] -> is 和 == 的区别
is和==的区别 / Difference between is and == 对于Python的对象来说,具有id/type/value三种特性,而在判断两个相等的is和==中,分别是对对象的id和 ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...
- Unity防破解 —— 加密Dll与Key保护
在阅读这篇文章之前,我在处理mono加密问题时,也是参考了雨凇的文章,所以建议先看一下雨凇写的关于加密Dll的文章: 1.Unity3D研究院之Android加密DLL与破解DLL .SO 2 ...
- hdu6040
hdu6040 题意 将一个函数运行 n 次,一共得到 n 个值,有 m 次询问,每次询问第 k 小的值. 分析 考察了 \(nth\_element\) 函数的运用.\(nth\_element(a ...
- 数学【P2524】 Uim的情人节礼物·其之弐 (康托展开)
因为某人@ZAGER挖坑让我讲一下康托展开,所以发现了这个题,顺便说一下康托展开是个什么东西 题目概括 给定n与一个数列,要求求出给定数列在n的全排列中的排名(按照字典序从小到大排列) 康托展开 先放 ...
- Palindrome Permutation -- LeetCode
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- luogu P1373 小a和uim之大逃离
题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...
- 事务没有提交导致 锁等待Lock wait timeout exceeded异常
异常:Lock wait timeout exceeded; try restarting transaction 解决办法: 执行select * from information_schema.i ...
- sqlserver 巧用REVERSE和SUBSTRING实现lastindexof
原文:sqlserver 巧用REVERSE和SUBSTRING实现lastindexof select REVERSE(SUBSTRING(REVERSE(testFixtureNumber),0, ...