Objective-C Learning 1

Source: Internet
Author: User

First of all, to learn objective-C (hereinafter referred to as OC) Well, you must have the foundation of C language, because OC is a superset of C language and OC is a process-oriented language. [C language is an object-oriented language]. Many syntaxes in OC are similar to those in C language. OC can be fully compatible with C language to achieve mixed Compiling. To learn OC well, it is best to have a C language basis. (If you have C ++ basics or ideas, you can learn OC better)

1. All files in OC are stored as. M files, and Declaration files are. H files like C files;

Personal reminder: whether it is C or oC, it is best to use the camper ID for some identifiers;

Creation class:

Class Name:GenerallyUppercase;

To define a class:1. Define the class name; 2. Compile the class attributes ---> (instance variable); 3. Compile the class behavior ---> method;

Tip: Generally, the class name should be the same as the file name for programmers to understand. The attribute declaration in a general class starts with an underscore because the global variable is declared here, convenience: duplicate the keyword in the local variable; only variables can be declared here, and no value can be assigned;

Method definition:

-(Void) method: (INT) arguments;

-(Method type: instance method, also called object method)

+ (Class method)

(Void) return value; no return value is void;

Method method name;

(INT "type"): parameter types are included;

Arguments: parameter name;

-------

The method definition is divided into: Without parameters, such as-(void) Shoping;

With one parameter: void (Shoping) :( float) price;

There are two parameters: void (shopping) :( float) price :( float) weight;

-----------------------------

Method implementation:@ Implementation in. m

// Write the implementation of the method here;

-(Void) Shopping :( float) price {

Nslog (@ "I'm shopping! ");

}

@ End;

And then apply it to the main function.

# Import "person. H"

Int main (){

//Create an object using the class name;

// Person * person = [person alloc];

// Person = [person init];

//Alloc applies for a space area in the memory to store the object information;

// Call the init method below to initialize some default data;

Person * person = [[person alloc] init]; // nest two steps together;

//The variables are declared through class type on the left, and the objects are created on the right;

++ ++

// The previous section defines the class and creates an object with the class;

---------------

There is also a method for creating objects:

Person * person2 = [person new]; (this method is not recommended in OC)

This method is generally used: person * person1 = [[person alloc] init];

_______________________

Call methods in the object;

-The instance method (or object method) starts with +: class method;

-Objects must start with person1)

Run the following command in main:
[Person1 shopping: 200]; // 200 is a parameter
It will enter the shopping function;
// This indicates that the shopping instance method is called; the instance method must have an object before it can be called;
// Many methods can be created through the class;

----------------------------------

After creating the object person1;

Set the name and age;

No way to directly set the name and method, onlySet attributes using methods;

Method To set the name:-(void) setname :( nsstring *) Name; // method description:Set attributesName;

Implementation: In the. M file:

-(Void) setname :( nsstring *) name {

_ Name = Name;

}

----------------------------------

Return to main and set the name:

[Person1 setname: @ "Zhang San"]; // input the Zhang San string to the name function, and then pass the name of Zhang San to the _ name function;

-----------------------------------

Attribute: it is also called a member variable, a global variable, or an instance variable. global variables can be used in all methods;

----------------

The method used to set attributes is called the setter method or the set method;
The method for obtaining attributes from the outside is called the get method, which has a return value;

-----------------------------------

For example,-(nsstring *) Name;
 
-(Nsinteger) age;
Implementation:
-(Nsstring *) name {
Return _ name;
}
-(Nsinteger) Age {
Return _ age;
}
Call: nsstring * name1 = [person1 name]

++ ++

It used to be a parameter method. Learn it todayMultiple Parameter Methods;

-----------------

-(Void) setname :( nsstring *) Name setage :( nsinteger *) age; // method name has two parts: setname and setage. setage can be saved here (but not recommended );

Implementation:-(void) setname :( nsstring *) Name setage :( nsinteger *) Age {

_ Name = Name;

_ Age = age;

} // This is not generally defined here. It is only used for demonstration;Generally, a variable defines a setting method.;

Call: [person1 setname: @ "Zhang San" setage: 22]; [object call Method !]

----------------

[Switch between. M and. H] command + cotrl + up and down keys. The Tab key can complete the keywords or identifiers we have written;

----------------

Method Type:-object method, + class method;

+: Class Method

Call the method using the class name; call it a class method;
Method Description: used to create a person object;
Define the class method: + (person *) newperson;
Implementation Method: + (person *) newperson {
Person * P = [[person alloc] init];
// Some variables can be initialized here
// [P setname: @ "zhangsan" setage: 22]
}
Call class: person * person3 = [person newperson];

--------------------------------

If no object is created, properties cannot be used;

No instance objects. Where are the attributes?

Objects must be available for use;

// Attributes cannot be accessed in class methods;

Attribute: who calls the method attribute and belongs to whom; ++ ++

Create a complementary object;
When an object is created, its member variables are not initialized. What are their default values?
@ Interface person: nsobject
Int _ age;

// The system initializes it as byte: 0; short: 0; INT: 0; long 0l; CHAR: \ u0000 '; float: 0.0f; double: 0.0d; bollean: false; All pointers: NIL;

@ End

++ ++

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.