First recognized Objective-C, objectivec

Source: Internet
Author: User

First recognized Objective-C, objectivec

(Unlike Java learning, oyangjian's video learning notes)

1. In OC, the keywords start with @ (to avoid conflicts with C and C ++), such as @ class, @ interface, @ implementation, @ public ......

2, for (xx in xx), BOOL, YES, NO.

3. The base class-NSObject-single inheritance, the interface-supports the interface protocol-@ protocol, and the interface method can be implemented in an optional manner. Multiple inheritance can be implemented using the interface, supports polymorphism, static classes, and exceptions (@ try, @ catch, @ finally). All functions are virtual functions.

4. id type: In OC, each target table can fulfill the id type, which can be considered as NSObject * or void * (any pointer type ).

5, nil: equivalent to null, indicating a target pointer (null pointer ).

6. The OC class consists of two files:. h and. m .. H is the class declaration (function field, function Declaration), the specific implementation of the. m storage class.

Class Declaration uses the keyword @ interface @ end.

Class implementation is implemented with the keyword @ implementation @ end.

If you declare and implement a class function, you need to use + or-to start with the function: + to represent the class method (static method),-to represent the object method (dynamic method ).

<1> class declaration <Dog. h>

@ Interface Dog: NSObject {

// You can only write segments and attributes.

}

// Write the function declaration

@ End

<2> class implementation <Dog. m>

# Import "Dog. h"

@ Implementation Dog

// Write function implementation

@ End

7. Differences between import and include

# The disadvantage of include header files is that a header file may be contained multiple times.

# The advantage of import is that a single head file can only be contained once.

(

# Ifndef _ HEAD_H _

# Define _ HEAD_H _

 

# Endif

// C/C ++ contains the header file format

)

8. Create/destroy objects

// Create an object

Dog * dog = [Dog malloc];

// Initialize the constructor

[Dog init];

// Destroy the object

[Dog release];

9. OC variable Declaration

@ Public, @ pretected (default, default, protection type), @ private

All functions in OC are public (private and private can be implemented using other methods)

10. All OC classes and interface declarations must contain *. Here * represents both pointer and reference.

OC: Dog * myDog;

Here, * represents both the real wise and the reference, which can be accessed through myDog-> dog or myDog. dog.

11. A function defined in OC tired is generally not called a function or a message.

12. method definition:

-(Int) f :( int) x;

"-" Indicates the object method, "int" indicates the return value, "f" indicates the function name, and the second "int" indicates the parameter type. Multiple parameters are separated, the colon is part of the function name, and the second Colon: there must be a space before it.

************

<Demo. h>

# Import <Foundation/Foundation. h>

// Cube class declaration

@ Interface Cube: NSObject {

// @ Public

Int length;

Int width;

Int high;

}

//-(Void) setter :( int) length :( int) width :( int) high;

-(Int) retBulk :( int) length :( int) width :( int) high;

-(Void) print;

@ End

// Rectangular class declaration

@ Interface Rectangle: NSObject {

// @ Public

Float len;

Float w;

}

-(Float) retArea :( float) len :( float) w;

-(Float) retCirc :( float) len :( float) w;

@ End

************

<Demo. m>

//

// Demo3.m

// Demo3

//

// Created by qianfeng on 15/11/30.

// Copyright©2015 qianfeng. All rights reserved.

//

# Import "demo3.h"

// Cube implementation

@ Implementation Cube

/*

-(Void) setter :( int) length :( int) width :( int) high {

// Function

Self-> length = length;

Self-> width = width;

Self-> high = high;

Printf ("***** \ n ");

}

*/

-(Int) retBulk :( int) length :( int) width :( int) high {

Int bulk = 1;

Bulk = length * width * high;

Return bulk;

}

-(Void) print {

Int n [10], I, j, k, tmp;

For (I = 0; I <10; I ++ ){

Scanf ("% d", & n [I]);

}

For (I = 0; I <10-1; I ++ ){

For (j = 0; j <10-i-1; j ++ ){

If (n [j]> n [j + 1]) {

Tmp = n [j];

N [j] = n [j + 1];

N [j + 1] = tmp;

}

}

}

For (k = 0; k <10; k ++ ){

Printf ("% d", n [k]);

}

Printf ("\ n ");

}

@ End

// Rectangular implementation

@ Implementation Rectangle

-(Float) retArea :( float) len :( float) w {

Int a = 1;

A = len * w;

Return;

}

-(Float) retCirc :( float) len :( float) w {

Int c = 0;

C = 2 * len + 2 * w;

Return c;

}

@ End

************

<Main. m>

# Import <Cocoa/Cocoa. h>

# Import "demo3.h"

Int main (int argc, const char * argv []) {

// Printf ("######### \ n ");

// NSLog (@ "Hello, World! ");

// Cube * d = [Cube new];

// [D setter: 1: 2: 3];

// NSLog (@ "% d \ n", d-> length, d-> width, d-> high );

// Printf ("% d \ n", d-> length, d-> width, d-> high );

// Create two cube objects with the sizes of 4, 5, 6, 7, 8, and 9 respectively.

Cube * cube1 = [Cube new];

Cube * cube2 = [Cube new];

NSLog (@ "the volume of the 1st cubes is % d \ n", [cube1 retBulk: 4: 5: 6]);

NSLog (@ "the volume of the 2nd cubes is % d \ n", [cube2 retBulk: 7: 8: 9]);

// Printf ("1st objects to volume: % d \ n", [d1 getter: 4: 5: 6]);

// Printf ("2nd objects to volume: % d \ n", [d2 getter: 7: 8: 9]);

// Input 10 numbers from the terminal and output them in ascending order

Cube * d4 = [Cube new];

// [D4 print];

 

Printf ("\ n ");

// Create two rectangles, with the length and width of 6, 7, 4.5, and 2.3 respectively. Calculate the area and perimeter of the two rectangles.

Rectangle * r1 = [Rectangle new];

Rectangle * r2 = [Rectangle new];

NSLog (@ "The area of the 1st rectangle is %. 2f \ n", [r1 retArea: 6: 7]);

NSLog (@ "1st rectangular perimeter: %. 2f \ n", [r1 retCirc: 6: 7]);

NSLog (@ "2nd rectangular area: %. 2f \ n", [r2 retArea: 4.5: 2.3]);

NSLog (@ "2nd rectangular perimeter: %. 2f \ n", [r2 retCirc: 4.5: 2.3]);

 

Return 0;

}

 

************

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.