Declaration and definition of classes in OC Foundation--OC

Source: Internet
Author: User

Steps to design a class in OC:

First, the declaration of the class:

1. Keywords used [email protected] and @end

2. Class name

3. Inheriting NSObject

4. Properties

5. Method (behavior, declaration only)

II. realization (definition) class

1. Keywords used [email protected] and @end

2. Implementing the methods declared in @interface

3. Declaration and implementation of methods in a class be careful to start with the minus "-"

The parentheses in the 4.OC class cannot be used indiscriminately, and parentheses are used to enclose the type! --Remember!

Example: The following code declares and defines the class, the method in the class has no return value, no parameter

1*implementing the methods declared in @interface2*/3 4 #import<Foundation/Foundation.h>5 6 //declaration of the class7 @interfaceDog:nsobject8 //Properties9 {Ten     @public One     intSpeed ; A } -  - //just a statement . the- (void) run; -  - @end //Write @interface immediately after writing @end -  + //implementation of the Class (definition) - @implementationDog +  A //implementing the Run method at- (void) Run - { -NSLog (@"The speed of the%i dog ran up.", speed); - } - @end -  in intMain () - { toDog *dog = [dogNew]; +Dog->speed = -; -      the [dog run]; *      $     Panax Notoginseng     return 0; -}

The method name of the class in OC has the condition of the return value and the parameter:

1. One parameter corresponds to a colon

2. A colon is also part of the method name

1 /*2 Design a calculator class3 1. Class Name: Caculator4 2. Behavior (method):5 * Return to pi:3.146 * Calculates the square of a value7 * Calculation of two values and8  */9 Ten #import<Foundation/Foundation.h> One  A //Statement of the calculator - @interfaceCaculator:nsobject -  the //declaration of the method -- (Double) Pi;//method name Pi, return value type is double parenthesis enclosing type, no parameter -  - //-(Double) Pi: (int) ABC; //Method name pi: +  - //one parameter corresponds to a colon + //the colon is also part of the method name A- (Double) Pingfang: (Double) number;//method Name: Pingfang: at  - //method Name: sumofnum1:andnum2:--to enhance the readability of your code -- (Double) SumOfNum1: (Double) NUM1 andNum2: (Double) num2; - @end -  -  in intMain () - { toCaculator *CA = [caculatorNew]; +      -     //NSLog (@ "pi=%f", [CA PI]); the      *     //double d = [CA pingfang:4]; $     Panax Notoginseng     //NSLog (@ "4 squared =%f", d); -      the     Doublehe = [CA sumOfNum1:TenANDNUM2:5]; +  ANSLog (@"10+5=%f", he); the      +      -      $Caculator *CA2 = [caculatorNew]; $      -     return 0; - } the  - Wuyi //the implementation of the calculator the @implementationCaculator -  Wu //implementing the methods declared in @interface -- (Double) Pi About { $     return 3.14; - } -  -- (Double) Pingfang: (Double) number A { +     returnNumber *Number ; the } -  $- (Double) SumOfNum1: (Double) NUM1 andNum2: (Double) num2 the { the     returnNUM1 +num2; the } the  - @end

The difference between the function in OC and the OC object method:

1. Functions belong to the entire file, can be called anywhere in the file; Object methods belong to objects, only objects can call object methods

2. Object methods can only be declared between @interface and @end, and object methods can only be implemented between @implementation and @end.

The Declaration and definition of a function can be written anywhere, and the function cannot be owned by a class and belongs to only one file.

Declaration and definition of classes in OC Foundation--OC

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.