Objective-c language-Methods and file compilation

Source: Internet
Author: User

OC Method and file compilation

First, OC method

(i) Object methods

(1) Object method with-opening as-(void) xx;

(2) Object methods can only be called by object

(3) The member variables of the current object can be accessed in the object method

(4) Call format [Object Name object method name];

(5) Design a student class and dog class, practice the use of object methods.

Statement of Student class:

The realization of student class:

Statement of the Dog category:

The implementation of the Dog class:

Main program:

(ii) Class methods

(1) class method with + beginning such as + (void) put;

(2) class methods can only be called by a class

(3) Class methods cannot access instance (member) variables because class methods are called again, and no storage space is created to store member variables in the class.

(4) Call format: [Class name class method name];

(5) Benefits and applications of the class method:

Do not depend on the object, the execution is more efficient;

Can use the class method solves the problem, uses the class method as far as possible;

Occasion: You can change to a class method when you do not need to use a member variable inside a method.

(6) Design a calculator class, practice using the class method.

Statement of the Calculator class:

The implementation of the Calculator class:

Main program: Calling class methods directly using the class name

Note 1: You can allow class methods and object methods to have the same name.

Note 2: The class method can be called in the object method.

(iii) Method name

(1) method without parameters

Statement:

Call:

(2) method with parameters

Statement:

Call:

Note: The colon is also part of the method name.

Ii. Compilation of files

In the work, the different classes are usually placed in different files, each class declaration and implementation is separate, the declaration is written in the. h header file, the implementation is written in the corresponding. m file, the class name is what, the filename prefix is what.

Assuming there are two classes, the person class and the dog class, there are usually the following five files:

(1) Declaration file for the Person.h person class

(2) Implementation file for the PERSON.M person class

(3) Dog.h Dog class declaration file

(4) Dog.m the implementation file of the Dog class

(5) MAIN.M main function (program entry)

In the main function and the implementation file of the class, use #import to include the corresponding header file.

Add: Import has two functions: the same as include, the complete copy of the contents of the file, and the second is to automatically prevent the file contents of the duplicate copy (even if the file is included multiple times, and only one copy).

When compiling a linked file using the command line, Usually, the. m file is compiled, and then all the target files are linked, but in Xcode, all the. m files are compiled and linked, and if there is a duplicate definition of the error, then the majority of the problem is that the contents of the file are either duplicated or contain a. m file.

Either the Include or import in the source file cannot contain. m or. c files, and can only be declared. As a result, classes are usually split in OC and split into declarations and implementations of two parts.

Hint: This is also a embodiment of the idea of programming, can say. h and. m files are completely independent, only to require a good readability, only requires the file name of two files consistent, this is the interface and implementation separation, so that the caller does not have to care about the specific implementation details.

Xcode is a line to compile a line, there is a simple repair function, red is the error prompt, yellow warning. If a variable is declared in the program, but the variable is not used, a warning message is generated. When debugging the program, if you find the entire page is not an error, but a run is wrong, then must be a link error.

Objective-c language-Methods and file compilation

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.