[Objective-c 基础

A.封装内部细节,根据需求暴露方法

复制代码
 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Student : NSObject
 4 {
 5     int age;
 6 }
 7 
 8 - (void) setAge:(int) newAge;
 9 - (int) age;
10 
11 - (void) study;
12 
13 @end
14 
15 @implementation Student
16 
17 - (void) setAge:(int) newAge
18 {
19     if (newAge <= 0)
20     {
21         age = 1;
22     }
23     else
24     {
25         age = newAge;
26     }
27 }
28 
29 - (int) age
30 {
31     return age;
32 }
33 
34 - (void) study
35 {
36     NSLog(@"%d岁的学生在学习", age);
37 }
38 
39 @end
40 
41 
42 int main()
43 {
44     Student *stu = [Student new];
45     [stu setAge:21];
46     [stu study];
47    
48     NSLog(@"这个学生的年龄是%d", [stu age]);
49    
50     return 0;
51 }
复制代码
 
B.封装规范
使用下划线开头命名实例变量
复制代码
 1 @interface Student : NSObject
 2 {
 3     int _no;
 4     Sex _sex;
 5 }
 6 
 7 - (Sex) sex;
 8 - (void) setSex:(Sex) newSex;
 9 - (int) no;
10 - (void) setNo:(int) no;
11 
12 @end
复制代码
如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
原文地址:https://www.cnblogs.com/wvqusrtg/p/4501487.html