First define a human parent class
Definition:
1234567891011121314151617181920 |
// // Human.h // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> @interface Human : NSObject { BOOL sex; } +( void ) toString; -( void ) showSex; @end |
Note: the plus sign before + (void) indicates that this is a class method (static method), and-(void) indicates that this is an instance method.
Implementation:
Note: The following-(ID) Init is the constructor. Correspondingly, there is also a-(void) dealloc method to release resources (similar to the Destructor or the dispose () method in C #)-note: the dealloc method will be detailed in memory management.
123456789101112131415161718192021222324252627282930313233343536 |
// // Human.m // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "Human.h" @implementation Human // Constructor -( id ) init { NSLog (@ "init() in Human is called" ); sex = TRUE; return ( self ); } // Static method + ( void )toString { NSLog (@ "this is a class method of Human" ); } // Instance method - ( void )showSex { NSLog (@ "my sex is %@" ,[email protected] "MALE" :@ "FEMALE" ); } @end |
Define a woman subclass.
Definition:
1234567891011121314151617181920212223 |
// // Woman.h // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> #import "Human.h" @interface Woman : Human { BOOL married; } -( void ) canCook:( NSString *) foodName; -( void ) setMarried:( BOOL )m; -( BOOL ) Married; @end |
Implementation:
Note the following: setmarried and married are standard attributes in obj-C (of course, other simplified attributes can be viewed later)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
// // Woman.m // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "Woman.h" @implementation Woman // Woman class Constructor -( id ) init{ NSLog (@ "init() in Woman is called!" ); if ( self ==[ super init]){ sex = FALSE; married = FALSE; } return ( self ); } // Overwrite tostring () in the parent class () +( void )toString { NSLog (@ "This is Woman‘s ToString()" ); } // Woman can cook -( void )canCook:( NSString *) foodName { NSLog (@ "I can cook %@" ,foodName); } // Setter of the attribute -( void ) setMarried:( BOOL )m { married = m; } // Getter of the attribute -( BOOL ) Married { return married; } @end |
Call in the main method:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
#import <Foundation/Foundation.h> #import "Human.h" #import "Woman.h" int main ( int argc, const char * argv[]) { NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init]; // insert code here... NSLog (@ "Hello, World!" ); // Call the "static" method of the class [Human toString]; NSLog (@ "----------------" ); // Create a human instance Human *man = [Human new ]; // Call man's showsex Method [man showSex]; NSLog (@ "----------------" ); // Define a woman subclass instance Woman *wife = [Woman new ]; [wife canCook:@ "Rice" ]; // Call the method inherited from the parent class [wife showSex]; // Set attributes [wife setMarried:TRUE]; // Read the attribute value NSLog (@ "wife‘s married = %@" ,wife.Married== NO [email protected] "FALSE" :@ "TRUE" ); NSLog (@ "----------------" ); // Call the tostring method after Overwrite [Woman toString]; // Common methods in the factory mode, which are still applicable here (except that 'human 'may not respond to'-cancook: 'will be warned during compilation :') Human *wife2 = [Woman new ]; [wife2 canCook:@ "soap" ]; NSLog (@ "----------------" ); [pool drain]; return 0; } |
Running result:
17:01:02. 016 OOP [1725: a0f] Hello, world!
17:01:02. 053 OOP [1725: a0f] This is a class Method of Human
17:01:02. 062 OOP [1725: a0f] ----------------
17:01:02. 075 OOP [1725: a0f] Init () in human is called
17:01:02. 091 OOP [1725: a0f] My sex is male
17:01:02. 094 OOP [1725: a0f] ----------------
17:01:02. 099 OOP [1725: a0f] Init () in woman is called!
17:01:02. 104 OOP [1725: a0f] Init () in human is called
17:01:02. 105 OOP [1725: a0f] I can cook rice
17:01:02. 108 OOP [1725: a0f] My sex is female
17:01:02. 109 OOP [1725: a0f] Wife's married = true
17:01:02. 111 OOP [1725: a0f] ----------------
17:01:02. 116 OOP [1725: a0f] This is woman's tostring ()
17:01:02. 120 OOP [1725: a0f] Init () in woman is called!
17:01:02. 121 OOP [1725: a0f] Init () in human is called
17:01:02. 123 OOP [1725: a0f] I can cook soap
17:01:02. 125 OOP [1725: a0f] ----------------