07- Definition and use of methods

1. Define a class, declare the method in the .h file of the class, and implement the method in the .m file
@interface 类名 : NSObject
{
Attributes Attributes represent characteristics of a class.
}
A declaration of a method; a method represents the functionality of the class.
@end
@implementation 类名
implementation of the method;
@end

The data type in the method header must be enclosed in parentheses.
- (return value type) method name: (parameter type) parameter name;

2. A method without parameters.

1). Statement
a. Location: outside the curly braces of @interface.
b. Syntax:
- (return value type) method name;
- (void)run;
Indicates that a method with no return value and no parameters is declared. The method name is called run
2). Realize
a. Location: Implemented in @implementation
b. Implemented syntax:
Copy the declaration of the method into @implementation. Remove the semicolon and add braces 1 to the code that implements the method
Written in curly brackets.
3). Call
a. The method cannot be called directly. Because the class cannot be used directly. The object must be created first.
Then the object has the properties and methods of the class, and the method of the object can be called.
b. Invoke the method of the object.
[object name method name];
3. A method with 1 parameter.
1). Statement
a. Location: outside the curly braces of @interface.
b. Syntax:
- (return value type) method name: (parameter type) formal parameter name;
- (void)eat:(NSString *)foodName;
A method is defined that has no return value.
The name of this method is eat:
This method has 1 parameter, the type is NSString * The name of the parameter is called foodName
- (void)eat:(NSString *)foodName;
2). Realize
a. Location: Implemented in @implementation
b. Syntax: Copy the declaration of the method into @implementation. Remove the semicolon and add braces 1 to the code that implements the method
Written in curly brackets.
3). Call
a. The method cannot be called directly. Because the class cannot be used directly. The object must be created first.
Then the object has the properties and methods of the class, and the method of the object can be called.
b. Calling syntax:
[对象名 方法名:实参];

4、带多个参数的方法.
1) 声明
a.位置: 在@interface的大括弧的外面.
b. 语法:
- (返回值类型)方法名称:(参数类型)形参名称1 :(参数类型)参数名称2 :(参数类型)参数名称3;
- (int)sum:(int)num1 :(int)num2;
表示声明了1个方法 这个方法的返回值类型是int类型的.
方法的名称叫做 sum: :
有两个参数 参数类型都是int类型 参数名称叫做num1 num2
2).实现.
a. 位置: 在@implementation之中实现
b. 实现的语法: 将方法的声明拷贝到@implementation之中.去掉分号 追加大括弧1对 将方法
实现的代码写在大括弧之中.
3).调用:
a. 方法是无法直接调用的.因为类是不能直接使用的.必须要先创建对象.
那么这个对象中就有类中的属性和方法了 就可以调用对象的方法了.
b. 调用带多个参数的语法
[对象名 方法名:实参1 :实参2 :实参3];

5、带参数的方法声明的规范:
1). 如果方法只有1个参数. 要求最好这个方法的名字叫做 xxxWith:
xxxWithxxx
eatWith:
eatWithFood:
这样写的话,那么调用方法的时候看起来就像是1条完整的语句. 提高了我们代码的阅读性.
遵守的规范: 就是让我们的方法调用的时候看起来像1条完整的语句.
2).如果方法有多个参数 建议这个方法名称:
方法名With:(参数类型)参数名称 and:(参数类型)参数名称 and:(参数类型)参数名称;
- (int)sumWith:(int)num1 and:(int)num2;

3).如果方法的返回值是当前类的对象,那么方法的返回值就写instancetype
更详细的写法
方法名With参数1:(参数类型)参数名称 and参数2:(参数类型)参数名称 and参数3:(参数类型)参数名称;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901727&siteId=291194637