1.封装的注意点
1: // 成员变量尽量不要用@public
2: // @public
3: int age;
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: //@public
2: // 只读(readonly):只允许外界访问我的no,不允许外界修改我的no
3: int no; // 只需要提供get方法
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2.get、set方法
1: set方法
2: 1.作用: 提供一个方法给外界设置成员变量值,可以在方法里面对参数进行相应过滤
3: 2.命名规范:
4: 1> 方法名必须以set开头
5: 2> set后面跟上成员变量的名称,成员变量的首字母必须大写
6: 3> 返回值一定是void
7: 4> 一定要接收一个参数,而且参数类型跟成员变量类型一致
8: 5> 形参的名称不能跟成员变量名一样
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: get方法
2: 1.作用:返回对象内部的成员变量
3: 2.命名规范:
4: 1> 肯定有返回值,返回值类型肯定与成员变量类型一致
5: 2> 方法名跟成员变量名一样
6: 3> 不需要接收任何参数
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1:
2: // set方法的实现
3: - (void)setAge:(int)newAge
4: {
5: // 对传进来的参数进行过滤
6: if (newAge <= 0)
7: {
8: newAge = 1;
9: }
10:
11: age = newAge;
12: }
13:
14: - (int)age
15: {
16: return age;
17: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
3.成员变量的命名规范:
一定要以下划线 _ 开头
作用:
1.让成员变量和get方法的名称区分开
2.可以跟局部变量区分开,一看到下划线开头的变量,一般都是成员变量
例子
1: // sex的set和get方法
2: - (void)setSex:(Sex)sex;
3: - (Sex)sex;
4:
5: // no的set和get方法
6: - (void)setNo:(int)no;
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
4.封装的实际例子
1: /*
2: 4.设计一个成绩类
3: * C语言成绩(可读可写)
4: * OC成绩(可读可写)
5: * 总分(只读)
6: * 平均分(只读)
7: */
8: #import <Foundation/Foundation.h>
9:
10: @interface Score : NSObject
11: {
12: int _cScore; // C语言成绩
13: int _ocScore; // OC成绩
14:
15: int _totalScore;// 总分
16: int _averageScoe; // 平均分
17: }
18:
19: - (void)setCScore:(int)cScore;
20: - (int)cScore;
21:
22: - (void)setOcScore:(int)ocScore;
23: - (int)ocScore;
24:
25: - (int)totalScore;
26: - (int)averageScore;
27:
28: @end
29:
30: @implementation Score
31: - (void)setCScore:(int)cScore
32: {
33: _cScore = cScore;
34:
35: // 计算总分
36: _totalScore = _cScore + _ocScore;
37: _averageScoe = _totalScore/2;
38: }
39: - (int)cScore
40: {
41: return _cScore;
42: }
43:
44: - (void)setOcScore:(int)ocScore
45: {
46: _ocScore = ocScore;
47:
48: // 计算总分
49: _totalScore = _cScore + _ocScore;
50: _averageScoe = _totalScore/2;
51: }
52: // 监听成员变量的改变
53:
54: - (int)ocScore
55: {
56: return _ocScore;
57: }
58:
59: - (int)totalScore
60: {
61: return _totalScore;
62: }
63: - (int)averageScore
64: {
65: return _averageScoe;
66: }
67: @end
68:
69:
70: int main()
71: {
72: Score *s = [Score new];
73:
74: [s setCScore:90];
75: [s setOcScore:100];
76:
77: [s setCScore:80];
78:
79:
80: int a = [s totalScore];
81:
82: NSLog(@"总分:%d", a);
83:
84: return 0;
85: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }