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