Swift Section Five(对象与类)

对象与类


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

//: Playground - noun: a place where people can play

import UIKit

//swift 第五课 对象与类

//1.类的创建方法

class Shape {

var numberOfSides = 0//属性的声明与变量常量的声明方式一致

let numberOfBadges = 1

var id: Int?

func simpleDescription() ->String {//方法同样一致

return "A shape with \(numberOfSides) sides."

}

func setupShapeId(id: Int) {

self.id = id

}

}

var shape = Shape()

shape.numberOfSides = 7

var description = shape.simpleDescription()

shape.setupShapeId(1993)

//2.类的初始化方法

class NamedShape {

var numberOfSides: Int = 0

var name: String

init(name: String){

self.name = name

}

func simpleDescription() ->String {

return "A shape with \(numberOfSides) sides."

}

}

源代码请前往我的github

Comments