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.
- 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.
- -(Id) init // initialize the object
- {
-
- If (self = [super init]) {
- // Initialize the content
- }
- Return self;
- }
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!