OBJECT-C Study notes (1)

Source: Internet
Author: User

Description: I use the WIN8 system installed Code::Blocks practice OC.

1. Diagram of the Declaration and implementation of the class:

@interface's like exposing the outside clock surface

@implementation is like a structure hidden inside the clock.


2. Places to be aware of in grammar

(1). Member variables cannot be initialized in {}, and cannot be directly taken out of the interview

(2). Method cannot be called as a function

(3). member variable \ Method cannot be modified with static and other keyword, cannot be mixed with C language

(4). The implementation of the class can be written behind the main function, mainly after the declaration.

3. Some of the wrong wording:

(1). There are only class declarations, no class implementations

(2). Missed @end

(3) [email protected] and @implementation nested

(4). Declaration Nesting of two classes

(5). Member variables are not written in brackets

(6). The Declaration of the method is written in the curly brace face

differences in 4.OC methods and functions

(1). The OC method can only be declared between @interface and @end, but only between @implementation and @end. This means that the OC method cannot exist independently of the class

(2) C functions do not belong to the class, and the class is not associated with the C function only to define the function of the file all

(3) C function cannot access members of OC object

(4) Low-level error: The method has a declaration, but the implementation of the time written as a function

5. Oc method to note the place:
(1). method is only declared, not implemented (classic error)
(2). method is not declared, only implementation (compiler warning, but can call, OC's weak syntax)
(3). Compile the time: visit no member variable directly error, access to ask no method, just a warning

6. Special attention should be paid to:

(1). A colon is also part of the method name
(2). The same class does not agree with two object methods with the same name

7. Code Practices:

#import <foundation/foundation.h>/* Calculator class */@interface calculator:nsobject{    @public    double num;} Computes the sum of squares-(double) square;-(int) SUMWITHNUM1: (int) NUM1 addNum2: (int) num2; @end//Calculator implementation @implementation calculator// Computes the square of a number-(double) square{    return num * num; Sum of two numbers-(int) SUMWITHNUM1: (int) NUM1 addNum2: (int) num2{   return num1 + num2;} @endint Main () {Calculator *c = [Calculator new];//assigns a value to the Calculator class member variable C->num = 25;//calls the square this method double result = [C Square]; NSLog (@ "%.1f squared is%.1f", c->num,result);//Call sum method: int sum = [c sumwithnum1:23 addnum2:34]; NSLog (@ "sum=%d", sum); return 0;}


Code:




OBJECT-C Study notes (1)

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.