-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14633841095158.html
More file actions
564 lines (384 loc) · 17 KB
/
14633841095158.html
File metadata and controls
564 lines (384 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
GeekBand.Swift-面向对象 - Zhan's Blog
</title>
<link href="atom.xml" rel="alternate" title="Zhan's Blog" type="application/atom+xml">
<link rel="stylesheet" href="asset/css/foundation.min.css" />
<link rel="stylesheet" href="asset/css/docs.css" />
<script src="asset/js/vendor/modernizr.js"></script>
<script src="asset/js/vendor/jquery.js"></script>
<script src="asset/highlightjs/highlight.pack.js"></script>
<link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
function before_search(){
var searchVal = 'site:swuzjb.github.io ' + document.getElementById('search_input').value;
document.getElementById('search_q').value = searchVal;
return true;
}
</script>
</head>
<body class="antialiased hide-extras">
<div class="marketing off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<nav class="top-bar docs-bar hide-for-small" data-topbar>
<section class="top-bar-section">
<div class="row">
<div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
<ul id="main-menu" class="left">
<li id=""><a target="_self" href="index.html">Home</a></li>
<li id=""><a target="_self" href="archives.html">Archives</a></li>
<li id=""><a target="_blank" href="aboutMe.html">RESUME</a></li>
</ul>
<ul class="right" id="search-wrap">
<li>
<form target="_blank" onsubmit="return before_search();" action="http://google.com/search" method="get">
<input type="hidden" id="search_q" name="q" value="" />
<input tabindex="1" type="search" id="search_input" placeholder="Search"/>
</form>
</li>
</ul>
</div></div>
</div>
</section>
</nav>
<nav class="tab-bar show-for-small">
<a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
<span> Zhan's Blog</span>
</a>
</nav>
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a href="index.html">HOME</a></li>
<li><a href="archives.html">Archives</a></li>
<li><a href="about.html">ABOUT</a></li>
<li><label>Categories</label></li>
<li><a href="iOS%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.html">iOS学习笔记</a></li>
<li><a href="Java.html">Java</a></li>
<li><a href="%E8%AF%BE%E7%A8%8B%E7%9B%B8%E5%85%B3.html">课程相关</a></li>
<li><a href="%E7%AE%97%E6%B3%95-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.html">算法-数据结构</a></li>
<li><a href="%E5%85%B6%E5%AE%83.html">其它</a></li>
</ul>
</aside>
<a class="exit-off-canvas" href="#"></a>
<section id="main-content" role="main" class="scroll-container">
<script type="text/javascript">
$(function(){
$('#menu_item_index').addClass('is_active');
});
</script>
<div class="row">
<div class="large-8 medium-8 columns">
<div class="markdown-body article-wrap">
<div class="article">
<h1>GeekBand.Swift-面向对象</h1>
<div class="read-more clearfix">
<span class="date">2016/5/16</span>
<span>posted in </span>
<span class="posted-in"><a href='iOS%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.html'>iOS学习笔记</a></span>
<span class="comments">
<a class="ds-thread-count" data-thread-key="14633841095158.html" data-count-type="comments" href="14633841095158.html#ds-thread"></a>
</span>
</div>
</div><!-- article -->
<div class="article-content">
<blockquote>
<p>面向对象三大特性:封装、继承、多态</p>
</blockquote>
<h2 id="toc_0">封装</h2>
<p>封装值得是隐藏类的一部分内部结构,避免外部访问或者外部直接访问,只有类自身可以操作。<br/>
通常,我们使用<code>getter</code>和<code>setter</code>来达到封装的目的。</p>
<h3 id="toc_1">访问控制符</h3>
<ol>
<li>private —— private访问级别所修饰的属性或者方法只能在当前的Swift源文件里可以访问。</li>
<li>internal(默认访问级别,internal修饰符可写可不写)—— internal访问级别所修饰的属性或方法在源代码所在的整个模块都可以访问。如果是框架或者库代码,则在整个框架内部都可以访问,框架由外部代码所引用时,则不可以访问。如果是App代码,也是在整个App代码,也是在整个App内部可以访问。</li>
<li>public —— 可以被任何人使用</li>
</ol>
<h3 id="toc_2">一个例子</h3>
<p>假设我们想要创建一个代表圆的类,那么圆的半径应该是可以改变的。而且,该圆的面积和周长应该可以从圆的实例中获取,而这两个属性不应该被类本身以外所更改。出于性能考虑,面积和周长也应该只计算一次。</p>
<pre><code class="language-swift">class Circle {
private var area: Double = 0;
private var diameter: Double = 0;
var radius: Double {
didSet {
calculateFigures();
}
}
init(radius:Double) {
self.radius = radius;
calculateFigures();
}
private func calculateFigures() {
area = M_PI * radius * radius;
diameter = 2 * M_PI * radius;
}
func getArea() -> Double {
return self.area;
}
func getDiameter() -> Double {
return self.diameter;
}
}
</code></pre>
<p>Swift还为我们提供了简便方法。<br/>
通过在属性前面使用<code>private(set)</code>,属性就被设置为默认访问等级的 <code>getter</code>方法,但是<code>setter</code>方法是私有的。所以我们可以去掉两个 <code>getter</code>方法:</p>
<pre><code class="language-swift">class Circle {
private(set) var area: Double = 0;
private(set) var diameter: Double = 0;
var radius: Double {
didSet {
calculateFigures()
}
}
init(radius:Double) {
self.radius = radius
calculateFigures()
}
private func calculateFigures() {
area = M_PI * radius * radius
diameter = 2 * M_PI * radius
}
}
</code></pre>
<p>当然也可以为属性设置公有的getter方法:</p>
<pre><code class="language-switf">public private(set) var area: Double = 0
public private(set) var diameter: Double = 0
</code></pre>
<h2 id="toc_3">继承</h2>
<p>面向对象中,非常重要的一个概念。通过继承,子类可以拥有父类(超类)中的方法和属性,对代码进行复用。<br/>
在Swift中,子类继承父类的属性、方法、下标。</p>
<h3 id="toc_4">一个例子</h3>
<pre><code class="language-swift">struct Point{
var x=0;
var y=0;
}
class Shape{
var no=0;
func move() {
print("NO: \(no) Shape.move");
}
}
class Rectangle: Shape{
var leftUp=Point();
var width=0;
var height=0;
}
class Circle: Shape{
var center=Point();
var radius=0;
}
</code></pre>
<p>Rectangle和Circle就继承了Shape中的方法和属性。</p>
<h3 id="toc_5">方法重写</h3>
<p>有的时候,我们需要在子类中对父类的某一个方法进行加工。就需要进行方法的重写。在Swift中,对于方法的重写,必须添加Override关键字。</p>
<pre><code class="language-swift">class Shape{
var no=0;
//用final关键字修饰,子类无法重写
final func show(){
print("Shape.show");
}
func move() {
print("Shape.move");
}
}
class Rectangle: Shape{
//重写父类的no属性
override var no: Int {
get{
print("Rectangle.no.get()");
return super.no;
}
set{
print("Rectangle.no.set()");
super.no=newValue;
}
}
override func move() {
print("Rectangle.move");
}
}
class Circle: Shape{
override func move() {
print("Circle.move");
}
}
</code></pre>
<h3 id="toc_6">继承中的初始化器和析构器</h3>
<h4 id="toc_7">初始化器</h4>
<ul>
<li>如果子类没有定义初始化器,则自动继承父类的初始化器</li>
<li>如果子类定义了初始化器,则不再继承。此时子类的初始化器必须调用父类的一个初始化器。如果手工不调用,编译器将自动生成调用。</li>
<li>如果子类的初始化器与父类的初始化器原型一致,必须使用override</li>
<li>在子类中使用父类属性,必须确保首先调用父类初始化器</li>
</ul>
<h4 id="toc_8">析构器</h4>
<ul>
<li>如果子类没有定义析构器,会自动继承父类的析构器</li>
<li>子类析构器执行完毕后,会自动调用父类析构器</li>
<li>子类析构器自动具有多态性</li>
</ul>
<h2 id="toc_9">多态</h2>
<p>多态,也是面向对象的三大特性之一。即父类引用指向子类对象。在Swift中,每个属性都有两个类型,一个是声明类型,一个是实际类型。这两个类型在值类型中是相等的。但是在类中,可能不同。但是实际类型一定是声明类型本身或其子类。</p>
<pre><code class="language-swift">//如下代码是没有问题的
var rect:Shape;
rect = Rectangle();
//当我们调用rect的move方法的时候,实际调用的是Rectangle中的Move方法。
rect.move();
</code></pre>
<p>也可以将多态描述为:子类在同一行为接口下,表现不同的实现方式。</p>
</div>
<div class="row">
<div class="large-6 columns">
<p class="text-left" style="padding:15px 0px;">
<a href="14634889513152.html"
title="Previous Post: GeekBand.Swift-协议">« GeekBand.Swift-协议</a>
</p>
</div>
<div class="large-6 columns">
<p class="text-right" style="padding:15px 0px;">
<a href="14633830220167.html"
title="Next Post: GeekBand.Swift-结构与枚举">GeekBand.Swift-结构与枚举 »</a>
</p>
</div>
</div>
<div class="comments-wrap">
<div class="share-comments">
<div class="ds-thread" data-thread-key="14633841095158.html" data-url="http://swuzjb.github.io/14633841095158.html" data-title="GeekBand.Swift-面向对象"></div>
</div>
</div>
</div><!-- article-wrap -->
</div><!-- large 8 -->
<div class="large-4 medium-4 columns">
<div class="hide-for-small">
<div id="sidebar" class="sidebar">
<div id="site-info" class="site-info">
<div class="site-a-logo"><img src="asset/img/icon.jpg" /></div>
<h1>Zhan's Blog</h1>
<div class="site-des">展镜博的个人博客</div>
<div class="social">
<a class="github" target="_blank" href="https://github.com/swuzjb" title="GitHub">GitHub</a>
<a class="email" href="mailto:412008196@qq.com" title="Email">Email</a>
<a class="rss" href="atom.xml" title="RSS">RSS</a>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>Categories</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="iOS%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.html"><strong>iOS学习笔记</strong></a>
<a href="Java.html"><strong>Java</strong></a>
<a href="%E8%AF%BE%E7%A8%8B%E7%9B%B8%E5%85%B3.html"><strong>课程相关</strong></a>
<a href="%E7%AE%97%E6%B3%95-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.html"><strong>算法-数据结构</strong></a>
<a href="%E5%85%B6%E5%AE%83.html"><strong>其它</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>BookList</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="bookList.html"><strong>待读书单</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>Recent Posts</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="15090149503358.html">Jupyterhub安装流程</a>
</li>
<li class="post">
<a href="15090148978006.html"></a>
</li>
<li class="post">
<a href="15037351230027.html">Java研发工程师的面试宝典</a>
</li>
<li class="post">
<a href="14896338034345.html">设计模式-适配器模式</a>
</li>
<li class="post">
<a href="14894720983586.html">设计模式-命令模式</a>
</li>
</ul>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>FRIEND LINKS</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="https://guxinyan.github.io">顾鑫燕的博客</a>
</li>
<li class="post">
<a href="http://ranchu.win">CD'S Blog</a>
</li>
<li class="post">
<a href="https://hran.me">Hran's Blog</a>
</li>
</ul>
</div>
</div>
</div><!-- sidebar -->
</div><!-- hide for small -->
</div><!-- large 4 -->
</div><!-- row -->
<div class="page-bottom clearfix">
<div class="row">
<p class="copyright">Copyright © 2016 swuzjb
</div>
</div>
</section>
</div>
</div>
<script type="text/javascript">
var duoshuoQuery = {short_name:'zhanjingbo'};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<script src="asset/js/foundation.min.js"></script>
<script>
$(document).foundation();
function fixSidebarHeight(){
var w1 = $('.markdown-body').height();
var w2 = $('#sidebar').height();
if (w1 > w2) { $('#sidebar').height(w1); };
}
$(function(){
fixSidebarHeight();
})
$(window).load(function(){
fixSidebarHeight();
});
</script>
<script src="asset/chart/all-min.js"></script><script type="text/javascript">$(function(){ var mwebii=0; var mwebChartEleId = 'mweb-chart-ele-'; $('pre>code').each(function(){ mwebii++; var eleiid = mwebChartEleId+mwebii; if($(this).hasClass('language-sequence')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = Diagram.parse($(this).text()); diagram.drawSVG(eleiid,{theme: 'simple'}); }else if($(this).hasClass('language-flow')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = flowchart.parse($(this).text()); diagram.drawSVG(eleiid); } });});</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?0fb9ecff8c9a2c820ce81c18ff77c1e8";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>