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 ...
随机推荐
- UVA 1395 Slim Span 最小生成树
题意: 给你一个图,让你求这个图中所有生成树中满足题目条件的,这个条件是生成树中最长边与最短边的差值最小. 思路: 根据最小瓶颈生成树的定义:在一个有权值的无向图中,求一个生成树最大边的权值尽量小.首 ...
- Python的并发并行[3] -> 进程[1] -> 多进程的基本使用
多进程的基本使用 1 subprocess 常用函数示例 首先定义一个子进程调用的程序,用于打印一个输出语句,并获取命令行参数 import sys print('Called_Function.py ...
- Python的网络编程[0] -> socket[1] -> socket 模块
socket 1. 常量 / Constants AF_* 和 SOCK_* 分别属于 AddressFamily 和 SocketType 1.1 AF_*类常量 socket.AF_UNIX: ...
- adb 查看android手机中应用的包名和安装位置
1. 查看是否连接手机 adb devices 2. 进入指定的device的shell adb shell 或者 adb -s ********* shell 3. adb 查看所有安装的包 pm ...
- IOS8设备连接XCODE6真机调试报错"Could not inspect the application package"
每次真机调试都要运行2次才行,解决办法:把product name改成英文就行了.
- linux之ls指令的关键字过滤显示
假设~/admin的文件夹下面有几个文件: a.pm b.pm c.pl d.pl e.pml 想只显示.pm结尾的可以用: ls *.pm 或者 ls | grep .pm,两者等价. 想显示b.开 ...
- #if 条件编译
1.格式: #if constant-expression statements #elif constant-expression statements #else statements #endi ...
- window coordinate
到ndc的转换是通过(x/w,y/w,z/w) 到了 window coordinates dx upper left gl lower left setviewport setscissor rt ...
- ElasticSearch _xpack角色管理
权限管理可以通过kibana的Management界面进行,本篇主要介绍的是通过命令进行,角色API使您能够在本机域中添加,删除和检索角色. 要使用此API,您必须至少具有manage_securit ...
- 2017.4.18 静态代码分析工具sonarqube+sonar-runner的安装配置及使用
配置成功后的代码分析页面: 可以看到对复杂度.语法使用.重复度等等都做了分析,具体到了每一个方法和每一句代码. 四种使用方式: sonarqube + sonar-runner sonarqube + ...