Objective-C Constructor

Source: Internet
Author: User
Tags c constructor

Objective-C ConstructorThe content to be introduced in this article is as follows:Objective-cWe can useConstructorIt is a special method used to initialize object data when creating an object.ConstructorYou can name them in any way, but generally, they are named Init.

ConstructorReturns a pointer to an object. We can call the init method of the superclass to obtain that the pointer superclass is the class derived from the current class, that is, the parent class of the current class, here is generally the NSObject class );

Reference code:

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

In code, when we create an object, we can pass the value to the constructor. For example, the following code initializes the data in the object to: 3

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

The following is the reference code for a complete example:

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

The running result in the Console window is as follows:

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

Summary:Objective-C ConstructorI hope this article will help you. MoreObjective-CFor more information, see edit recommendations.

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.