Objective-C -- class, method, attribute, and member variable
I have had the basics of related programming languages. I believe that I understand the concepts of classes and objects in C ++/java. I will not repeat it too much here.
For the definition and method usage of classes in OC, OC defines a set of its own specifications.
Class Definition:
@ Interface FirstClass: NSObject
@ End
// @ Interface indicates that a class is declared, ":" indicates the inheritance relationship, and @ end indicates the end of the class.
Class implementation
@ Implementation FirstClass
Method declaration:
+ (Void) print;
-(Id) init;
// "+" Indicates that a class method is declared and called by Lei Ming.
// "-" Indicates the instance method declared, which must be called by the Class Object
// When calling a method without parameters, the hidden self parameter is used to access the attributes of instance variables.
To declare a method with parameters:
+ (Void) initWithName :( type) variable name and :( type) variable name A :( type) variable name;
The method declaration with parameters in OC is odd. The red part is the method name, indicating that the parameter is followed. That is, the actual name of the method is initWithNAme: and::
Method call:
[FirstClass print]; // call the class method FirstClass * node = [FirstClass alloc] init] through the class name; // generate the FirstClass object
[Node initWithName: @ "codebat" andAge: 21]; // call the Parameter Function
Attributes and variables
Attribute is a new mechanism of the oc language. We must declare the corresponding instance variables.
Member variable declaration:
@ Interface FirstClass: NSObject {// member variable of the class. The default access permission is protect int m; double n; char c; float f ;}
// Member variables are defined in {}. Once declared, they can be directly used in the. m file of the implementation file of this class, which is equivalent to the global variables in this class.
Attribute
// Member variables are defined in {}. Once declared, they can be directly used in the. m file of the implementation file of this class, which is equivalent to the global variables in this class.
Use @ property to define attributes in Declaration
@ Property (parameter) type variable name;
@ Property (nonatomic, strong); // use strong for non-basic types
@ Property (nonatomic, assign); // basic type assign
In the implementation section, use @ synthesize to merge attributes.
@ Synthesize variable name;
@ Synthesize name = _ name; // _ name is an instance of name.
Parameters:
Read/write attributes: readwrite/readonly
Memory attribute: assign/retain/copy/strong; copy is content copy; retain is Pointer copy
Atomic attributes: atomic/noatomic. nonatomic prohibits multithreading and variable protection improves performance.
The three access control operators @ protected in OC are used to protect instance variables from being accessed only by methods defined in the class and its subclass. @ private is used to protect instance variables from being accessed only by methods in the class; @ public: The variable after this modifier can be directly accessed by all classes or methods defined by modules.