Dark Horse Programmer-ios Notes-Package construction method

Source: Internet
Author: User

We used the new method when we created the object with the OC class. In fact, the new method is a combined method, which is composed of two methods, the Alloc method and the Init method, respectively. Where the Alloc method is a class method, which is used to assign a storage space to an object, the Init method is an object method, which is used to initialize the object, and the Init method is also called the constructor method.

In OC, we usually rewrite the Init method to assign some value to the member variable at initialization time, so we'd better not use the new method when we create the object, because then we can't rewrite the constructor method. So how do you rewrite the Init method? Steps to override the Init method:

1. Call the Init method of the parent class. This is to initialize the member variables and other properties declared in the parent class first.

2. Determine if the object is initialized successfully, and then initialize the member variables of the sub-class.

3. Returns the Initialized object.

Here we create a person class, which defines a member variable _age of type int, and we create an object of the person class and initialize it so that the value of _age is 20.

[OBJC]View Plaincopy
  1. #import <Foundation/Foundation.h>
  2. Declaration of the Person class
  3. @interface Person:nsobject
  4. @property int age;
  5. @end
  6. Implementation of the Person class
  7. @implementation person
  8. Overriding the Init method
  9. -(ID) init
  10. {
  11. //Determine if the initialization of the parent class is complete, and initialize the _age if completed
  12. if (self= [Super Init]) {
  13. _age = 20;
  14. }
  15. //Returns the object after initialization is complete
  16. return self ;
  17. }
  18. @end
  19. int main ()
  20. {
  21. //Create an object of the person class
  22. Person *p = [[Person alloc] init];
  23. //Output _age value
  24. NSLog (@ "_age =%d", p. Age);
  25. return 0;
  26. }


Above is to initialize the P object of the person class and make the value of _age 20 after initialization is complete.

Override the Init method's point of note: First call the parent class's Init method [Super Init], and then initialize the member variables of the child class.

So can we not assign the initial value to the member variable in the class, but assign it somewhere else?

To redefine the Init method when assigning an initial value elsewhere, we will learn the initial value of the member variable passed in the main function.

[OBJC]View Plaincopy
  1. #import <Foundation/Foundation.h>
  2. @interface Person:nsobject
  3. @property int age;
  4. The redesign of the construction method requires a declaration
  5. -(ID) initwithage: (int) age;
  6. @end
  7. @implementation person
  8. Redesign the construction method and initialize the _age
  9. -(ID) initwithage: (int) age
  10. {
  11. if (self= [Super Init]) {
  12. _age = age;
  13. }
  14. return self ;
  15. }
  16. @end
  17. int main ()
  18. {
  19. //Create an object of the person class, call the redesigned construction method and pass in the value assigned to _age
  20. Person *p = [[Person alloc] initwithage:23];
  21. //Output _age value
  22. NSLog (@ "_age =%d", p. Age);
  23. return 0;
  24. }


The result of the above code output is:

_age = 23

The point of note for custom construction methods is:

1, beginning with "-", that is, must be the object method.

2, the return value is generally the ID type.

3, the method name usually begins with Initwith.

Let's learn how to design a subclass when inheriting a parent class.

[OBJC]View Plaincopy
  1. #import <Foundation/Foundation.h>
  2. Declaration and implementation of the person class
  3. @interface Person:nsobject
  4. @property int age;
  5. Declaration of the construction method of the design
  6. -(ID) initwithage: (int) age;
  7. @end
  8. @implementation person
  9. The implementation of design construction method
  10. -(ID) initwithage: (int) age
  11. {
  12. if (self= [Super Init]) {
  13. _age = age;
  14. }
  15. return self ;
  16. }
  17. @end
  18. Declaration and implementation of the student class
  19. @interface Student:person
  20. @property double score;
  21. Declaration of a design constructor
  22. -(ID) initwithage: (int) Age Andscore: (double) score;
  23. @end
  24. @implementation Student
  25. The implementation of design construction method
  26. -(ID) initwithage: (int) Age Andscore: (Double) score
  27. {
  28. //Call the parent class to re-design the construction method
  29. if (self= [Super Initwithage:age]) {
  30. _score = score;
  31. }
  32. return self ;
  33. }
  34. @end
  35. int main ()
  36. {
  37. //Create an object of the student class, call the redesigned construction method and pass in the values of _age and _score.
  38. Student *s = [[Student alloc] initwithage:18 andscore:92];
  39. //Output _age value
  40. NSLog (@ "_age =%d, _score =%.1f", S. Age, S. score);
  41. return 0;
  42. }


The emphasis of the above code is to call the constructor method of the parent class in the constructor of the subclass. In the design construction method, if the subclass needs to initialize the member variables of the parent class and the Init method that initializes the member variable is already defined in the parent class, we call the constructor of the parent class. That is, the attributes of the parent class are given to the parent class to handle, and the subclass to the subclass to handle.

The above is a simple introduction to the construction method. Let's summarize a few key points:

1. If you need to assign a value to a member variable at initialization time, we will rewrite the Init method, which is the constructor method.

2. When overriding the constructor method, make sure to invoke the constructor of the parent class.

3. When designing the construction method, remember that the attributes of the parent class are given to the parent class for processing.

4, the construction method must be an object method.

Dark Horse Programmer-ios Notes-Package construction method

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.