Introduction to OBJECTIVE-C-related classes, methods, properties, member variables

Source: Internet
Author: User

definition of Class
@interface Firstclass:nsobject
@end
//@interface means declaring a class, ":" Representing an inheritance relationship, @end the end of the class
implementation of the class
@implementation FirstClass
declaration of the method
+ (void) print;
-(ID) init;
//"+" means declaring a class method, called by Thunder
//"-" represents the time instance method of the declaration, which must be called by the object of the class
///without parameters when invoked, the properties of the instance variable are accessed using the hidden self parameter.
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 rather weird, and the red part is the method name, which indicates that followed by the parameter. That is, the actual name of the method is initwithname:and:a:
invocation of the method
[FirstClass print];
//Call class method by class name
FirstClass *node=[firstclass alloc]init];//to generate FirstClass objects
[Node initwithname:@ "Codebat" andage:21];//function that invokes arguments
Properties and Variables
attributes are a new mechanism in the OC language and require that we declare the corresponding instance variables.
declaration of member variables
@interface Firstclass:nsobject
{
//class member variable, default access permission is protect
int m;
double N;
char c;
float F;
}
The //member variable is defined in {} and can be used directly in the implementation file. m file of this class once it is declared, equivalent to the global variable inside this class
Properties
The //member variable is defined in {} and can be used directly in the implementation file. m file of this class once it is declared, equivalent to the global variable inside this class
using @property to define properties in the Declarations section
@property (parameter) type variable name;
@property (Nonatomic,strong);//strong for non-basic types
@property (nonatomic,assign);//Basic type assign
using the @synthesize composition property in the implementation section
@synthesize variable name;
@synthesize Name=_name;//_name is an instance of name
Parameters
read-write properties: Readwrite/readonly
Memory Properties: Assign/retain/copy/strong,copy is a content copy, retain is a pointer copy
Atomic properties: Atomic/noatomic,nonatomic prohibit multi-threading, variable protection for high performance
The three access control @protected in OC, the protection instance variable can only be accessed by the method defined in the class and its subclasses, @private, the protected instance variable can only be accessed by methods in that class; @public, The variables following the modifier can be accessed directly by all methods defined by the class or module.

Introduction to OBJECTIVE-C-related classes, methods, properties, member variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.