Combination and access methods of OOP in Objective-C learning notes

Source: Internet
Author: User

Objective-CLearning notesOOPComposite and accessMethodThis is what I will introduce.OOPWill remember the textbook StyleOOPConcept: encapsulation, inheritance, and polymorphism. Let's briefly talk about their differences.

The superficial interpretation encapsulation is to hide the data. The Inheritance means that the subclass inherits the methods and attributes of the parent class or interface, and achieves code reuse. polymorphism means reload and rewrite. Such an explanation is only the basic features of object-oriented programming, which is simple and cannot be understood. Let's take a look at the use of OOP described in this article.

 
 
  1. if(self = [ super init] {    … 

To perform one-time initialization for a superclass, call [super init]. The value id data returned by the init method, that is, the generic object pointer) describes the initialized object.

Assigning the result of [super init] to self is a standard practice of Objective-C. This is done to prevent the objects returned by the superclass during initialization from being different from the previously created objects.

 
 
  1. -(Id) init // initialize the object
  2. {
  3.  
  4. If (self = [super init]) {
  5. // Initialize the content
  6. }
  7. Return self;
  8. }

Access Method

Accessor method) is used to read or change the specific attributes of an object.

Setter method: mutator) is used to change the object state.

Getter method: The getter method provides a way to read object attributes by using the object code.

Note: When operating on attributes of other objects, you should always use the access method provided by the object and never directly change the values of other object attributes. For example, main () should not directly change the value of the engine attribute through car-> engine, but should be changed using the setter method.

Naming Convention: the setter method is named based on the name of the property it changes, and the prefix "set" is added ", the getter method only names the returned attributes without the get prefix ).

Summary:Objective-CLearning notesOOPComposite and accessMethodI hope this article will help you!

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.