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 ...
随机推荐
- MySQL读写分离-架构
MySQL读写分离-架构 简介 对于很多大型网站(pv值百万.千万)来说,在所处理的业务中,其中有70%的业务是查询(select)相关的业务操作(新闻网站,插入一条新闻.查询操作),剩下的则是写(i ...
- LIS【p1704】寻找最优美做题曲线
Description 洛谷OJ刷题有个有趣的评测功能,就是系统自动绘制出用户的"做题曲线".所谓做题曲线就是一条曲线,或者说是折线,是这样定义的:假设某用户在第b[i]天AC了c ...
- 数学【p1658】 购物
题目描述 你就要去购物了,现在你手上有N种不同面值的硬币,每种硬币有无限多个.为了方便购物,你希望带尽量少的硬币,但要能组合出1到X之间的任意值. 分析: 看到题解做法没有说出原理,所以尝试解释一下. ...
- 详谈Format String(格式化字符串)漏洞
格式化字符串漏洞由于目前编译器的默认禁止敏感格式控制符,而且容易通过代码审计中发现,所以此类漏洞极少出现,一直没有笔者本人的引起重视.最近捣鼓pwn题,遇上了不少,决定好好总结了一下. 格式化字符串漏 ...
- zend studio9.0.3破解及汉化 windons版
注册码: 34E606CF10C3E4CF202ABCEAA9B0B7A64DD2C5862A514B944AAAB38E3EB8A5F2CD735A2AB4CF9B952590EFA62BA0AB2 ...
- 【gcc】warning: control reaches end of non-void function
用gcc编译一个C程序的时候出现这样的警告: warning: control reaches end of non-void function 它的意思是:控制到达非void函数的结尾.就是说你的一 ...
- lock参数变化吗
多线程应用中经常使用lock,在使用这个关键字的时候,经常有个疑问,如果更改了当时的入参,那么是否会变化呢,下面通过代码实例测试一把 class Program { static void Main( ...
- The newly created daemon process has a different context than expected
Error: The newly created daemon process has a different context than expected. It won't be possible ...
- 彻底解决DZ大附件上传问题
个. 注意:很多人遇到修改php.ini后重应WEB服务后仍然不能生效.这种情况应该先确认一下所改的php.ini是不是当前PHP所使用的.您可以在WEB目录下建立一个php文件,内容很简单就一句话& ...
- ES6里关于函数的拓展(三)
一.箭头函数 在ES6中,箭头函数是其中最有趣的新增特性.顾名思义,箭头函数是一种使用箭头(=>)定义函数的新语法,但是它与传统的JS函数有些许不同,主要集中在以下方面: 1.没有this.su ...