It is easy to extend an existing class in C #, for example:
Public static class utils {public static void printtoconsole (this string strsrc) {console. writeline (strsrc );}}
In this way, a printtoconsole method is added to the string class. The usage is as follows:
Class mainclass {public static void main (string [] ARGs) {"Hello world! ". Printtoconsole ();}}
In objective-C, there is a similar solution: stringutils. h definition section
# Import <Foundation/Foundation. h> @ interface nsstring (extnsstring)-(void) printtoconsole; @ end
Explanation: @ interface nsstring (extnsstring) indicates that the extnsstring class will extend nsstring and add some general additional methods for it. Implementation of stringutils. m
# Import "stringutils. H" @ implementation nsstring (extnsstring)-(void) printtoconsole {nslog (@ "% @", self);} @ end
The usage is as follows:
# Import <Foundation/Foundation. h> # import "stringutils. H "int main (INT argc, const char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; nsstring * STR = @" Hello world! "; [STR printtoconsole]; [pool drain]; return 0 ;}
Note: If the extension method added by the developer in C # is the same as the existing method that comes with the. NET Framework, the existing method that comes with the system will prevail in actual operation. However In obj-C In this case, the newly added The cognominal method overwrites the original method of the system. And no prompt! A good habit is for all extension methods (including class names ), Add A special Prefix Or suffix, Avoid duplicate names . Next topic: partial class Developed by Asp.net Program Members know that the partial class in C # can easily Code Is distributed across multiple physical files, and the compiler automatically merges them during compilation. This is a great feature. During team development, I often divide different business modules of a class into several different physical files (such as class_jimmy.cs, class_mike.cs ...), then Jimmy only writes code in class_jimmy.cs. Mike only writes code in class_mike.cs, which can greatly reduce (or avoid) the final SVN submission and merge conflicts. On the surface, partial class and the extension method are two different concepts, but in obj-C, these two are actually one thing. Scenario: for example, if a mall System adds, deletes, and modifies products, I want to put them in the file product. h, and the processing of the order, I want to put it in the file order. h, but I want to logically classify these business-related processes into the same class BLL. h. Let's look at the method in obj-C: (mainly to see how several files are organized into a class. The code is just an example) 1, First define BLL. H (mainly used to put some member variables, basically just a shell)
# Import <Foundation/Foundation. h> @ interface BLL: nsobject {nsstring * connstr;}-(void) setconnstring :( nsstring *) connstring;-(nsstring *) connstring; @ end
Bll. m implementation
# Import "BLL. H "@ implementation bll-(void) setconnstring :( nsstring *) connstring {connstr = connstring;}-(nsstring *) connstring {return connstr;}-(void) dealloc {[connstr release]; [Super dealloc] ;}@ end
2, Define product. h to extend the BLL class.
# Import <Foundation/Foundation. h> # import "BLL. H "@ interface BLL (product)-(void) addproduct: (nsstring *) productname productno :( nsstring *) prono;-(void) deleteproduct :( nsstring *) productno; @ end
Product. m
# Import "product. H "# import" BLL. H "@ implementation BLL (product)-(void) addproduct: (nsstring *) productname productno :( nsstring *) prono {nslog (@" connstring = % @ ", connstr ); // output BLL. connstrnslog (@ "addproduct success! Productname: % @, productno: % @ ", productname, prono);}-(void) deleteproduct :( nsstring *) productno {nslog (@" connstring = % @", [self connstring]); // you can also use attributes to access nslog (@ "deleteproduct success! Productno: % @ ", productno);} @ end
3,Define order. h to continue to extend BLL class
# 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
Because both the product class and order class are extended from The bll class, these three classes are logically the same class BLL. Finally, let's take a look at how to use them:
# Import <Foundation/Foundation. h> # import "BLL. H "# import" product. H "# import" order. H "int main (INT argc, const char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; BLL * BLL = [BLL alloc] init]; bll. connstring = @ "I am connection string. "; [BLL addproduct: @" iphone4 "productno: @" 0001 "]; // call product. [BLL createorder: @ "0001" Quantity: 5]; [BLL deleteproduct: @ "0001"]; // call order. [BLL release]; [pool drain]; return 0 ;}
Running result:
22:29:30. 369 demo [1292: a0f] connstring = I am connection string.
22:29:30. 376 demo [1292: a0f] addproduct success! Productname: iphone4 and productno: 0001
22:29:30. 378 demo [1292: a0f] Thank you for order our product. productno: 0001, quantity: 5
22:29:30. 379 demo [1292: a0f] connstring = I am connection string.
22:29:30. 380 demo [1292: a0f] deleteproduct success! Productno: 0001
Many languages and technologies are truly "one-way, all-way". Maybe the design inspiration for "extension methods" and "classification" in C # Comes from objective-C.