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 ...
随机推荐
- HDU 1846 Brave Game【巴什博弈裸题】
Brave Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- Python的程序结构[5] -> 模块/Module[0] -> 内建模块 builtins
builtins 内建模块 / builtins Module 在Python的模块中,有一种特殊模块,无需导入便可以使用,其中包含了许多内建函数与类. builtins 模块内容 / builtin ...
- FZU-2270 Two Triangles(两个三角形全等)
原题地址: 题意: 给出n个点,有两个人,每个人可以选3个点,问有多少种情况是可以找出两个三角形,是可以通过旋转使其全等. 思路: 所以首先要是三角形即三点不能共线,其次要全等,即三条边对应相等, ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- chess(威佐夫博奕)
---恢复内容开始--- 链接:https://www.nowcoder.com/acm/contest/116/G来源:牛客网 题意:一个棋盘,老王和小人下棋,棋子只能往下或者往左或者往左下走,小人 ...
- LCA【p4281】[AHOI2008]紧急集合 / 聚会
Description 欢乐岛上有个非常好玩的游戏,叫做"紧急集合".在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等 ...
- POJ1273 Drainage Ditches (网络流)
Drainage Ditches Time Limit: 1000MS Memor ...
- Linq GroupJoin , Join
static void Main(string[] args) { List<Person> persons = new List<Person> { }, }, }; Lis ...
- Oracle 查询每天执行慢的SQL
本文转载自http://blog.itpub.net/28602568/viewspace-1364844/ 前言: 工作中的您是否有 想对每天慢的sql进行查询.汇总或者行优化等情况,如下SQL希 ...
- COCOS2d 标准 android.MK
LOCAL_PATH := $(call my-dir) include$(CLEAR_VARS) LOCAL_MODULE := game_shared PP_CPPFLAGS := -frtti ...
- 开始使用 Docker (Linux 上运行 SQL Server) 上的 SQL Server 容器 - SQL Server | Microsoft Docs
原文:开始使用 Docker (Linux 上运行 SQL Server) 上的 SQL Server 容器 - SQL Server | Microsoft Docs 快速入门:使用 Docker ...