OC object-oriented method

Source: Internet
Author: User
OC object-oriented method

Design a caculator calculator class, which has the computing function (behavior)

1. method without Parameters

Design a method to return pi

// Method declaration

-(Double) PI;

// Method implementation

-(Double) Pi

{

Return 3.1415926;

}

Method Declaration

 

Method call


2. Method with one parameter

 

Design a square Calculation Method

// Method declaration

-(Double) Square :( double) number;

// Method implementation

-(Double) Square :( double) Number

{

Return Number * number;

}

Method Declaration


Method call



3. Methods with multiple parameters

Design a calculation and Method

// Method declaration

-(Double) sumofnum1 :( double) num1andnum2 :( double) num2;

// Method implementation

-(Double) sumofnum1 :( double) num1 andnum2 :( double) num2

{

Returnnum1 + num2;

}

Method Declaration


Method call


4 method name note

Colons are also part of the method name

Two methods with the same name are not allowed in the same class.

 

Differences between 5oC methods and functions

The OC method can only be declared between @ interface and @ end, but only between @ implementation and @ end. That is to say, the OC method cannot exist independently of the class.

 

C functions do not belong to the class and are not associated with the class. C functions are only owned by the file defining the function.

 

C functions cannot be accessed.) member variables of C objects.

 

Low-level error: methods are declared, but are implemented as functions.

 

 


OC object-oriented method

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.