Objective-C Constructor

Source: Internet
Author: User
Tags c constructor

Objective-C ConstructorIs the content to be introduced in this article, mainly to understandConstructorIt is a special method used to initialize object data when an object is created. The constructor returns a pointer to the object. We can call the init method of the superclass to obtain this pointer.Objective-cFunction,Objective-C Constructor,Objective-CFunction call,Objective-CFunction declaration.

Constructor is a special method used to initialize object data when creating an object. Constructors can be named in any way, but usually they are named Init. The constructor returns a pointer to an object. We can call the init method of the superclass to obtain this pointer. The superclass is the class derived from the current class, that is, the parent class of the current class, this is generally the NSObject class.

Objective-C Constructor

Reference content is as follows:

 
 
  1. -(Container * ) myInit(int)n {      
  2. self = [super init];      
  3. if(self)   {            
  4. [self  setNumber:n];     
  5. }       
  6. return selef;   

In the Code, when we create an object, we can pass the value to the constructor method). The following code initializes the data in the object to 3.

Reference content is as follows:

 
 
  1. Container* obj = [[Container new] myInit:3]; 

Complete instance reference code

Reference content is as follows:

 
 
  1. # Import <Foundation/Foundation. h>
  2. # Import "student. h"
  3. @ Interface myobj: NSObject {
  4. Int number;
  5. }
  6. -(Void) setNumber :( int) Num :( int) Num2;
  7. -(Void) outP;
  8. -(Myobj *) myinit :( int) Num :( int) Num2;
  9. @ End @ implementation myobj {}
  10. -(Myobj *) myinit :( int) Num :( int) Num2 {
  11. Self = [super init];
  12. // The Name Of The superclass Init method cannot be changed.
  13. If (self ){
  14. [Self setNumber: Num: Num2];
  15. }
  16. Return self;
  17. }
  18. -(Void) setNumber :( int) Num :( int) Num2 {
  19. Number = Num + Num2;
  20. }
  21. -(Void) outP {
  22. Printf ("this is the number you put in = % I", number );
  23. } @ End int main (int argc, const char * argv []) {
  24. NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
  25. Myobj * obj = [[myobj new] myinit: 10: 20];
  26. [Obj outP];
  27. [Pool drain];
  28. Return 0;
  29. }

Running results in the Console window:

Reference content is as follows:

 
 
  1. run [Switching to process 643]   
  2. Running…   
  3. this is the number you put in =30 
  4. Debugger stopped.   
  5. Program exited with status value:0.  

Summary: The Objective-C constructor has been described in detail. I hope this article will help you!

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.