OC-class, oc-Class Method

Source: Internet
Author: User
Tags c hello world

OC-class, oc-Class Method

1. header files
# Include <stdio. h>
# Import <Foundation/Foundation. h>

Difference: # import commands are faster and more efficient. # The include command tells the compiler to copy and paste the contained content to the target file. # Import checks whether the file has been imported or included in the target file.

Then observe the two Headers
Foundation. h we call it the main header file. The main header file copies the header files of all tools in the Toolkit. We only need to import the main header file to use all the tools in the Toolkit, avoid importing a corresponding header file every time you use it.
Rule: the names of all master header files are the same as those of the Toolbox.
All the main header files are imported to the header files of all tools in the toolbox.

2. The first oc Program

1/* 2 because OC is fully compatible with C, therefore, you can write C language code 3 in the OC program and combine the C language source files with the OC source files to generate executable files 4 */5 # import <Foundation/Foundation. h> 6 int main (int argc, const char * argv []) {7 printf ("c hello world \ n"); 8/* 9 differences between printf and NSLog: 10 NSLog will automatically wrap 11 NSLog some system information will be appended when the output content 12 NSLog and printf received parameters are different 13 */14 NSLog (@ "OC hello World "); 15 16 return 0; 17}

3. oc type

In C language, functions are divided into statements and implementations, and classes defined in OC are also divided into statements and implementations.

1) class declaration

// Starts with @ interface, ends with @ end, and then writes the thing name, that is, the class name, in the corresponding place of the class name. // the purpose of declaring a class is to tell the system, which of the following attributes are included in this class? // attributes in the OC class declaration can only be @ interface Person: NSObject {// note in the {} between @ interface and @ end: by default, the attribute in the OC object is int _ height that cannot be directly accessed; // Note: When writing the OC class attribute, we recommend that you add _ int _ weight; int _ age;} before all attribute names. // behavior @ end

2) class implementation

// Start with @ implementation and end with @ end. Then, write the name of the class declared when the class is declared, must be the same as the declared class name @ implementation Person // implementation of behavior @ end

3) method description and implementation

@ Interface Person: NSObject {// Code omitted here} // behavior/* the behavior in the OC is the same as the function in the C language. It is used to save a specific function and define a function in the C language, it can be divided into statements and implementations, and the statements are written in. h. in c, OC defines a method, which is also divided into declaration and implementation, declaration written in @ interface, and implementation written in @ implementation c language functions are divided into two types: methods In the internal function and external function OC are also divided into two types. Class methods and object method class methods can only be called by class names, and object methods can only be represented by class methods in the object call OC, the object method in OC uses-to indicate that the OC method is the same as that in C: 1. determine the function name 2. confirm parameters 3. confirm return value 4. determine the return value type * // Note: If there is no form parameter, you do not need to write (), but directly write one; in the OC method () is to put the data type-(void) about; @ end // class implementation @ implementation Person // behavior implementation-(void) about {NSLog (@ "This person information");} @ end


4) main Function

Int main (int argc, const char * argv []) {// you want to create an object through a class in OC, the syntax for sending a message/message to a class must be [class name/Object Name method name];/* If a class calls the new method of the class, that is, the process of the system creating an object after sending a message called new to the class is as follows: 1. allocate storage space for objects created for the Person class. initialize the attributes of the object created by the Person class. returns the address of the object created by the Person class * // receives the address of the Person object through a Person pointer // saves the address of the Person object using the Person pointer, in this case, the pointer p of the Person type is called the class in the Person object // OC. In essence, it is a struct, therefore, the p pointer actually points to a struct Person * p = [Person new]; // p-> _ height = 70; we can do this in C, however, in oc, direct access to member variables is not allowed,

[P about]; // call the object Method

    return 0;}


Finally, let's talk about Objective-C naming rules.

The variables pointing to the instance are named "camel case. Start with a lowercase letter, followed by the first letter of each word in uppercase.

The method name is also spelled out by camels.

The class name starts with an uppercase letter, but the following words continue to be spelled by camels.


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.