码迷,mamicode.com
首页 > 其他好文 > 详细

A Swift Tour(4) - Objects and Classes

时间:2014-06-08 00:44:09      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Objects and Classes(对象和类)

用 class 关键字后面跟一个类名来创建一个class,在一个类中声明 常亮或变量,他存在于当前类的上下文,函数的方法是同样的

var numberOfSides = 0
    let numberOfSidesLet = 1
    
    func simpleDescription() -> String
    {
        return "A shape with \(numberOfSides)  \(numberOfSidesLet) sides."
    }
    

通过括号的方式来创建一个类实例,使用点语法来访问该实例的属性和方法

var shape = Shape()
shape.numberOfSides=10
var str = shape.simpleDescription()
println(str)

吐槽一下,不知道是xcode6 bate版本问题还是什么原因,写代码的提示功能特别差

这个版本的一个重要的修改:在创建的时候设置初始值的项,使用init来创建,如下:

bubuko.com,布布扣
 1 class Shape {
 2     
 3     var name:String
 4     
 5     init(name:String)
 6     {
 7         self.name = name
 8     }
 9     
10     var numberOfSides = 0
11     let numberOfSidesLet = 1
12     
13     
14     func simpleDescription() -> String
15     {
16         return "A shape with \(numberOfSides)  \(numberOfSidesLet) sides."
17     }
18 }
bubuko.com,布布扣

请注意 self 关键字用来区分 属性name 和 参数 name(这个和oc中的还是一样)

如果你要释放一些对象,那么需要创建一个deinitializer,使用deinit来释放资源

子类和父类之间用 冒号分开,在继承标准的子类时,不需要声明,所以可以根据需要来忽略或者包括父类

子类重写父类的方法要使用overside关键字(C#,Java比较相似),如果没有重载,则会提示错误

bubuko.com,布布扣
class Square: Shape {
   
    var sideLength: Double
    
    init(sideLength:Double,name:String)
    {
        self.sideLength = sideLength
        super.init(name:name)
        numberOfSides = 4
    }
    
    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)
bubuko.com,布布扣

除了简单的属性,属性也可以有getter 和 setter方法

bubuko.com,布布扣
 1 class EquilateralTriangle: Shape {
 2    
 3     var sideLength:Double = 0.0
 4     
 5     init(sideLength:Double,name:String)
 6     {
 7         self.sideLength = sideLength
 8         super.init(name:name)
 9         numberOfSides=3
10     }
11     
12     var perimeter:Double
13     {
14         get{
15             return 3.0*sideLength
16         }
17         set
18         {
19             sideLength = newValue/3.0
20         }
21     }
22     
23     override func simpleDescription()->String
24     {
25         return "An equilateral triagle with sides of length \(sideLength)"
26     }
27 }
28  var triangle = EquilateralTriangle(sideLength:3.1,name:"a triangle")
29  println(triangle.perimeter)
30  triangle.perimeter = 9.9
31  println(triangle.sideLength)
bubuko.com,布布扣

在perimeter的setter方法中,新值得隐式名称是newValue,你可以在setter之后提供一个名字

初始化EquilateralTriangle类有三步:

1. 设置属性的值

2. 调用父类的构造方法(init)

3. 改变父类定义的属性值,其他的方法也可以在这里设置

如果你不需要计算属性,但是在setter之前或者之后执行,可以使用willSet和didSet,例如:下面的类永远保证三角形的边长等于正方形的边长

bubuko.com,布布扣
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:10,name:"ray test shape")
        println(triangleAndSquare.square.sideLength)
        println(triangleAndSquare.triangle.sideLength)
        triangleAndSquare.square = Square(sideLength:50,name:"larger square")
        println(triangleAndSquare.triangle.sideLength)
        println(triangleAndSquare.square.sideLength)
//打印出来的值为:10.0,10.0,50.0,50.0
bubuko.com,布布扣

函数和方法有一个不同点,函数的参数名只能在函数中使用,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(这个不知道怎么翻译)

bubuko.com,布布扣
 1 class Counter {
 2    
 3     var count:Int = 0
 4     func incrementBy(amount:Int,numberOfTimes times:Int)
 5     {
 6         count += amount*times
 7     }
 8 }
 9  var counter = Counter()
10  counter.incrementBy(2,numberOfTimes:7)
11         
bubuko.com,布布扣

当使用可选值时,可以像方法属性一样在操作符前使用问号(?),如果值本来就是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.

1  let optionalSquare :Square?=Square(sideLength:2.5,name:"optional square")
2  let sideLength = optionalSquare?.sideLength  //注意:等号和optionalSquare之间必须有空格,不知道编译器为什么会这样

 

A Swift Tour(4) - Objects and Classes,布布扣,bubuko.com

A Swift Tour(4) - Objects and Classes

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/mlgblog/p/3774516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!