Objective-c -- class, method, attribute, and member variable

Source: Internet
Author: User

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

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 initwithnameanda.

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;
<PRE name = "code" class = "objc"> @ property (nonatomic, strong) nsstring * Name; // <span style = "font-family: Arial, Helvetica, sans-serif; "> Use strong for non-basic types </span>
@ Property (nonatomic, assign) int count; // <span style = "font-family: Arial, Helvetica, sans-serif;"> Use assign for basic types </span>
 
</PRE> <PRE name = "code" style = "font-size: 13px;"> In the implementation section, use @ synthesize to synthesize 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.

Objective-c -- class, method, attribute, and member variable

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.