Objects and Classes(对象和类)
用 class 关键字后面跟一个类名来创建一个class,在一个类中声明 常亮或变量,他存在于当前类的上下文,函数的方法是同样的
var numberOfSides =
let numberOfSidesLet = func simpleDescription() -> String
{
return "A shape with \(numberOfSides) \(numberOfSidesLet) sides."
}
通过括号的方式来创建一个类实例,使用点语法来访问该实例的属性和方法
var shape = Shape()
shape.numberOfSides=
var str = shape.simpleDescription()
println(str)
吐槽一下,不知道是xcode6 bate版本问题还是什么原因,写代码的提示功能特别差
这个版本的一个重要的修改:在创建的时候设置初始值的项,使用init来创建,如下:
class Shape { var name:String init(name:String)
{
self.name = name
} var numberOfSides =
let numberOfSidesLet = func simpleDescription() -> String
{
return "A shape with \(numberOfSides) \(numberOfSidesLet) sides."
}
}
请注意 self 关键字用来区分 属性name 和 参数 name(这个和oc中的还是一样)
如果你要释放一些对象,那么需要创建一个deinitializer,使用deinit来释放资源
子类和父类之间用 冒号分开,在继承标准的子类时,不需要声明,所以可以根据需要来忽略或者包括父类
子类重写父类的方法要使用overside关键字(C#,Java比较相似),如果没有重载,则会提示错误
class Square: Shape { var sideLength: Double init(sideLength:Double,name:String)
{
self.sideLength = sideLength
super.init(name:name)
numberOfSides =
} func area() -> Double
{
return sideLength * sideLength
} override func simpleDescription() -> String
{
return "A square with sides of length \(sideLength)"
}
} var square = Square(sideLength:10.1,name:"my test")
square.area()
var str = square.simpleDescription()
println(str)
除了简单的属性,属性也可以有getter 和 setter方法
class EquilateralTriangle: Shape { var sideLength:Double = 0.0 init(sideLength:Double,name:String)
{
self.sideLength = sideLength
super.init(name:name)
numberOfSides=
} var perimeter:Double
{
get{
return 3.0*sideLength
}
set
{
sideLength = newValue/3.0
}
} override func simpleDescription()->String
{
return "An equilateral triagle with sides of length \(sideLength)"
}
}
var triangle = EquilateralTriangle(sideLength:3.1,name:"a triangle")
println(triangle.perimeter)
triangle.perimeter = 9.9
println(triangle.sideLength)
在perimeter的setter方法中,新值得隐式名称是newValue,你可以在setter之后提供一个名字
初始化EquilateralTriangle类有三步:
1. 设置属性的值
2. 调用父类的构造方法(init)
3. 改变父类定义的属性值,其他的方法也可以在这里设置
如果你不需要计算属性,但是在setter之前或者之后执行,可以使用willSet和didSet,例如:下面的类永远保证三角形的边长等于正方形的边长
class TriangleAndSquare { var triangle:EquilateralTriangle
{
willSet
{
square.sideLength = newValue.sideLength
}
} var square:Square
{
willSet
{
triangle.sideLength = newValue.sideLength
}
} init(size:Double,name:String)
{
square = Square(sideLength:size,name:name)
triangle = EquilateralTriangle(sideLength:size,name:name)
}
} var triangleAndSquare = TriangleAndSquare(size:,name:"ray test shape")
println(triangleAndSquare.square.sideLength)
println(triangleAndSquare.triangle.sideLength)
triangleAndSquare.square = Square(sideLength:,name:"larger square")
println(triangleAndSquare.triangle.sideLength)
println(triangleAndSquare.square.sideLength)
//打印出来的值为:10.0,10.0,50.0,50.0
函数和方法有一个不同点,函数的参数名只能在函数中使用,but parameters names in methods are also used when you call the method (except for the first parameter). By default, a method has the same name for its parameters when you call it and within the method itself. You can specify a second name, which is used inside the method(这个不知道怎么翻译)
class Counter { var count:Int =
func incrementBy(amount:Int,numberOfTimes times:Int)
{
count += amount*times
}
}
var counter = Counter()
counter.incrementBy(,numberOfTimes:)
当使用可选值时,可以像方法属性一样在操作符前使用问号(?),如果值本来就是nil,那所有在?之后的代码将会忽略,整个表达式都是nil,Otherwise, the optional value is unwrapped, and everything after the ? acts on the unwrapped value. In both cases, the value of the whole expression is an optional value.
let optionalSquare :Square?=Square(sideLength:2.5,name:"optional square")
let sideLength = optionalSquare?.sideLength //注意:等号和optionalSquare之间必须有空格,不知道编译器为什么会这样