Static methods and Instance methods
A method is the behavior of a class, written in an interface and implemented in two files. The method is declared in the interface section, and the implementation is partially implemented.
1. Class method and Example method
Classes in OBJECTIVE-C can declare two types of methods: instance methods and Class methods. An instance method is a method that executes within the scope of a specific instance of a class. In other words, before you invoke an instance method, you must first create an instance of the class. And the class method, by contrast, means that you do not need to create an instance.
The + represents a class method, which is a class-calling method that does not rely on a method with any object, similar to the static keyword static function of the C language. -Represents a method that is an instance (object) method that can only be called by an object, dependent on any object.
Summarize the points:
(1) class (Static) method (convenience builder)
A. Invocation of a class method
[Class name class method name];
Here's what to note:
1. Class methods can invoke class methods.
2. Class methods may not invoke instance methods, but class methods can access instance methods by creating objects.
3. Class methods are not allowed to use instance variables. The class method can use self because self is not an instance variable.
4, the class method as a message, can be sent to the class or object inside (in fact, can be called by the class or object means the method).
(2) Example method
A. Invocation of an instance method
You first need to instantiate the class
For example: Human *man = [Human alloc] init];
[Instance of Class
instance method name];
For example: [Man showsex];
Note: When the class is instantiated here, the constructor of the class is called, and the return value of the class's constructor call [Superinit] does not equal the self of the class.
Defining instances of Subclasses
Woman *wife = [Woman alloc] init];
When the class is instantiated here, the constructor of the class is called, and the return value of the class's constructor calls [Super init] equals the self of the class.
2. Single case mode
Singleton mode, which is a singleton mode. As the name implies, is mainly used to do the application of resource sharing control. In essence, a singleton is a class that has and has been instantiated only once in the program declaration cycle. To ensure that the instantiation is unique, use the class's Class (static) method to generate and access the object. At this point, we can access the singleton object of the class from anywhere in the program, because only the only one is instantiated, so the Alloc, init, Autorelease initialization method is not used.
Key points of the singleton design pattern:
(1) A class can have only one instance.
(2) He must create this object on his own
(3) This instance must be provided to the entire system on its own;
(4) This method must be a static class;
. m file code (remember)
@implementation Singleton
Static Singleton *st;
+ (singleton*) instance{//the available ID instead of returning the available pointer object instance is the method name
Static variables, the life cycle is the entire program, and can still be used the next time the function is called. if (St==nil) {//pointer is empty then create? st = [[Singleton alloc] init];
Pointer St points to a singleton mode Singleton
}
returnst;//is not empty, continue to use
}
@end
Note: The so-called Singleton, that is, we create a singleton class, the class can only generate a unique object, in order to prevent the object from being copied (copy) or retain and release, and so on, we must in the creation of the singleton implementation file (. m file) to the parent class of these methods to overwrite, The aim is to ensure a rigorous approach to the singleton model.
iOS Development--Grammar chapter OC & static Method and Example method