Objective-c oop (bottom)-class (static) method, instance method, overwrite (overwrite), property)

Source: Internet
Author: User

First define a human parent class

Definition:

 
/// Human. h // OOP // created by Jimmy. yang on 11-2-9. // copy 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)InitIt is a constructor. Corresponding, there is also a-(void)DeallocThe method is used to release resources (similar to the Destructor or the dispose () method in C #)-Note: The dealloc method will be learned in detail in memory management.

/// Human. M // OOP // created by Jimmy. yang on 11-2-9. // copy 2011 _ mycompanyname __. all rights reserved. // # import "human. H "@ implementation human // constructor-(ID) Init {nslog (@" Init () in human is called "); Sex = true; Return (Self );} // static class method + (void) tostring {nslog (@ "this is a Class Method of Human");} // instance method-(void) showsex {nslog (@ "my sex is % @", sex? @ "Male": @ "female");} @ end

Define a woman subclass.

Definition:

 
/// Woman. h // OOP // created by Jimmy. yang on 11-2-9. // copy 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:

NoteThe setmarried and married are standard attributes in obj-C (of course, other simplified attributes can be viewed later)

/// Woman. M // OOP // created by Jimmy. yang on 11-2-9. // copy 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-(void) setmarried :( bool) m {married = m;} of the attribute ;} // getter-(bool) married {return married;} @ end of the property

Call in the main method:

# Import <Foundation/Foundation. h> # import "human. H "# import" woman. H "int main (INT argc, const char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool 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 [wife showsex] inherited from the parent class; // set the attribute [wife setmarried: True]; // read the nslog (@ "Wife's married = % @", wife. married = no? @ "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] ----------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.