Static methods, class methods, instance methods

static modified local variables: extend the life cycle of layout variables
static modified global variables: the current variable can only be used in the current file
static modified function: the function can only be used in the current file

 

Static: A data structure that has the same life cycle as the program, exists from the time the program starts, and is destroyed when the program stops.

Static variables: There are two types of local static variables and global static variables, which must be modified with the "static" keyword.

Local static variables:
    Generally defined inside a method or function, modified with the static keyword;
    It will not be destroyed when the method or function call is completed, and its life cycle is the same as that of the program.
    Local static variables are initialized once.

void haha(){
   static  int a = 10 ;   // Only when the haha ​​function is called for the first time, the current code will be executed, and no matter how many times the haha ​​function is called after that, the current code will not be executed. 
  NSLog( @" %i " , a);
} // When the haha ​​function is called, it will not be destroyed.

Global static variable:
     Generally defined outside a method or function, not wrapped by any function or method, and modified with the static keyword.
     The scope of global static variables is the current file, and can only be used by methods or functions in the current file, and cannot be used by methods in other files.

---------------------Person.h-----------------------------------
static int a;
extern int b;
void haha(void);
@interface Person : NSObject
@property(nonatomic, copy) NSString *name;

@end
---------------------Person.m-------------------------------------
#import  " Person.h " 
static  int a = 10 ;   // Defined in Person.m file, can only be used in Person.m file, even if declared in h file, external files cannot access. 
extern  int b = 20 ; // Defined as a global variable and can be used in any file. 
void haha()
{
    NSLog(@"haha");
    NSLog(@"%i",a);
}
@implementation Person
@end

--------------------main.m---------------------------------------
int main(int argc, const char * argv[]) {
    NSLog(@"a=%i", a); // a=0
    NSLog(@"b=%i", b); // b=20
}

 

static and dynamic methods

There are no static methods in OC, i.e. methods decorated with " static " cannot be used in OC.
Methods that use static modification in other languages ​​are class methods. Therefore, the static method of OC can be called a class method in disguise, and the dynamic method is an instance method.

 

static and dynamic functions

A function decorated with the " static " keyword is a static function.
Features of static function: it can only be called in the file where it is defined, other files cannot be used, even if it is declared in the h file.
Dynamic functions can be used in any file as long as they are declared in the h file.
------------------Person.h----------------------------------
#import <Foundation/Foundation.h>
static void haha(void);
void hehe(void);
@interface Person : NSObject
@property(nonatomic, copy) NSString *name;
@end

------------------Person.m----------------------------------
#import "Person.h"
@implementation Person

void haha()
{
    NSLog(@"haha");
}

void hehe()
{
    NSLog(@"hehe");
}
@end

------------------main.m----------------------------------
int main(int argc, const char * argv[]) {
    hehe(); // Can be called. 
    haha(); // Program compilation error. 
}

 

In java, we already know that the methods in the class body are divided into two types: instance methods and class methods. Class methods are modified with static. What is the difference between the two? When a class creates an object, the object can call methods of the class.
When the bytecode file of the class is loaded into the memory, the instance method of the class will not be assigned the entry address. When the class creates the object, the instance method in the class will be assigned the entry address, so that the instance method can be used by any object created by the class. The object invokes the execution. It should be noted that when we create the first object, the instance method in the class is assigned the entry address, and when the object is created again, the entry address is no longer assigned, that is, the entry address of the method is shared by all objects. , when all objects do not exist, the entry address of the method is cancelled. For a class method in a class, the corresponding entry address is allocated when the class is loaded into memory. Therefore, class methods can not only be called and executed by any object created by the class, but also directly called by the class name. The entry address of the class method is not cancelled until the program exits. Class methods are assigned entry addresses when the bytecode of the class is loaded into memory. Therefore, the Java language allows direct invocation of class methods through class names, while instance methods cannot be invoked through class names. When we talked about classes, we emphasized that in the Java language, class methods in a class cannot operate instance variables, nor can they call instance methods. This is because before the class creates an object, the instance member variables have not allocated memory, and Instance methods also do not have entry addresses.
Secondly, only static members or methods can be called in static methods, and non-static methods or non-static members cannot be called. Non-static methods can call both static members or methods and other non-static members or methods.
class A { int x,y; static float f(int a){} float g(int x1,int x2){} } class B { public static void main(String args[]) { A a1=new A(); Af( 2 , 3 ); // Legal. a1.f( 2 , 4 ); // Legal. a1.g( 2 , 5 ); // Legal. Ag( 3 , 2 ); // Illegal. } }

Summary:
  1. Static methods and dynamic methods have different loading times.
  2. Only static methods or static members can be called inside a static method, and non-static methods or non-static members cannot be called.
  3. Static methods can only call non-static methods or non-static members by instantiating the object.
  4. Non-static methods can call both non-static methods and non-static members, as well as static methods or static members.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770672&siteId=291194637