OC point syntax and variable scopes
One, point grammar
(a) Understanding point grammar
Declare a person class:
#import <Foundation/Foundation.h>
@interface Person:nsobject
{
int _age;//defaults to @protected
}
-(void) Setage: (int) age;
-(int) age;
@end
Implementation of the person class:
#import "Person.h"
@implementation person
-(void) Setage: (int) Age
{
_age = age;//cannot be written as Self.age = NewAge, quite with [self setage:newage];
}
-(int) Age//get method
{
return _age;
}
@end
Use of point syntax:
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Insert code here ...
Person *person = [[Person alloc] init];
[Person Setage:10];
Person.age = 10;//point syntax, equivalent to [person setage:10];
This is not to assign a value to a person's attribute, but to invoke the Setage method of person
int age = [person age];
int age = person.age;//equivalent to int age = [person Age]
NSLog (@ "Age are%i", age);
[Person release];
}
return 0;
}
(b) The role of Point grammar
The purpose of the OC design point syntax is to allow other language developers to quickly start the OC language development, using DOT syntax, and making it look like other object-oriented languages such as Java.
(c) The essence of Point grammar
The essence of the point syntax is the invocation of the method, not the access to the member variable, which automatically expands to the appropriate method when the point syntax is used. Remember that the essence of point syntax is to convert to the appropriate set and get methods, and you cannot use point syntax without the set and get methods.
Such as:
stu.age=10; unfold as: [Stu setage:10];
int a=stu.age; expanded as: [Stu Age];
How does the compiler know whether it is a set method or a Get method? The main point is to look at the assignment (you can use breakpoint debugging to view it).
There is only one way to access a member variable in OC is to use-> such as Stu->age, which is required under the @public premise.
(iv) The use of Point syntax attention
The following usage is a dead loop:
(1) In the Set method, the Self.age=age is equivalent to [self setage:age];
(2) in the Get method, return Self.age, which is equivalent to [self age];
Ii. scope of the variable
(i) The scope of the variable is mainly divided into four kinds:
(1) @public (public) in the context of the object, anywhere can be directly accessed.
(2) @protected (protected) can only be accessed in the object method of the current class and subclass
(3) @private (private) can only be accessed directly in the object method of the current class
(4) @package (frame level) scope between private and public, as long as in the same frame can be accessed directly through the variable name
(ii) Use of attention and complement
(1) The member variable can also be declared in the implementation of the class, that is, the. m file, but the member variable declared here is @private because it usually contains only the header file in other files and does not contain the implementation file. The member variable defined in. m cannot drink its header file. h The member variable has the same name, and it is futile to use keywords such as @public during this period.
(2) The member variable declared between the @interface @end is protected by default if it does not make a special description.
(3) If a class inherits another class, it has all the member variables and methods of the parent class, noting that all the member variables have it, except that it is not directly accessible.
OC Object-Inheritance
I. Basic CONCEPTS
The world of the program and the human "object" of the world in the mind is no difference, the rich second-generation inherited parents, naturally have all the resources parents have, the subclass inherits the parent class also has all the methods and attributes (member variables).
Here animals are the parents of cats and dogs, and black and white cats are subclasses of cats.
Benefits of Inheritance:
(1) Extraction of Duplicate code
(2) Establish the relationship between class and class
Disadvantages of Inheritance:
Too strong coupling
Second, the succession in OC
@interface Animal:nsobject
The animal inherits the NSObject, obtains the NSObject class the method;
@end
@interface Dog:animal
Dog class inherits Animal class
@end
Note: The OC language is a single inheritance language. In the OC language, basically all of the class's root classes are nsobject classes.
Third, the use of inheritance attention
(1) The compiler executes from the top down, so at least there should be a declaration of the parent class at the front of the subclass;
(2) The member variable name with the same name is not allowed in OC and the parent class;
(3) The subclass in OC can have the same name as the parent, and when the subclass is called, take precedence over the internal search, if there is no one layer to look up;
Tip: Overriding is a subclass that implements a method in the parent class, overwriting the previous implementation of the parent class.
Schematic diagram: A total of three classes, the person class inherits the NSObject class, and the student class inherits the man class.
Create a Student *s=[[student alloc] init];
The student class and the parent class of the class are loaded into memory at this time.
Tip: Each class has a super class pointer that points to its own parent class. Object has an Isa pointer that points to the class that calls the object.
Iv. Inheritance and composition
Applications for Inheritance:
(1) When two classes have the same properties and methods, the same properties and methods can be extracted into a parent class.
(2) When Class A fully owns some of the properties and methods in class B, consider allowing Class B to inherit category A (consider), in which case a combination may also be considered.
Inheritance: # # #是xxx, such as dogs are animals, can let the dog inherit the animal class
Combination: # # #拥有xxx, such as students have books, you can let the book this class as a student class attributes
Five, the key word super
Super keyword, when overriding a method in a subclass, you can have the caller skip this layer and invoke the method in the parent class.
Role:
(1) Directly call one of the methods in the parent class
(2) When Super is in the object method, the object method of the parent class is called, and super is in the class method, then the class method of the parent class is invoked.
Usage Scenario: Subclasses want to preserve some of the behavior of the parent class when overriding the parent class method.