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之间必须有空格,不知道编译器为什么会这样

A Swift Tour(4) - Objects and Classes的更多相关文章

  1. 【读书笔记】A Swift Tour

    素材:A Swift Tour 推荐下载Playground:Download Playground objc 自己较为熟悉,想熟悉下风头正劲的 swift.就先从官方的入门手册开始撸. 每一小节,我 ...

  2. 冷市攻略:Listo 教你 25 今天的社会 Swift 语言 - 02 Swift Tour

    import Foundation //******************************************************************************** ...

  3. [IOS]《A Swift Tour》翻译(一)

    以下翻译内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3768936.html 碎碎念... Swift是苹果在WWDC刚发 ...

  4. Swift tour

    输出函数: print(“hello world!") 无需引入函数库,无须使用“;”作为语句结尾,也无须写跟其它语言一样的main()函数,Swift中,全局区的代码就是程序入口.You ...

  5. A Swift Tour(3) - Functions and Closures

    Functions and Closures 使用func来声明函数,通过括号参数列表的方式来调用函数,用 --> 来分割函数的返回类型,参数名和类型,例如: func greet(name: ...

  6. A swift Tour

    传统的认为,一个新的语言的第一个应用程序都会打印"Hellow,Word",在Swift中,可以只需要一行代码: pringln("Hello, word") ...

  7. Swift学习——A Swift Tour 函数

    Functions and Closures  函数和封闭性(闭包) Functions  函数的使用 Swift中的函数定义和OC中有明显的差别了,使用func定义函数,在括号里定义參数和类型,用 ...

  8. Swift学习——A Swift Tour 协议和扩展

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/28854395 Protocols an ...

  9. [Swift A] - A Swift Tour

    首先说下自己对Swift语言的一点点看法,对于一个写过javascript和常年写java代码的人来说,学习Swift是一件很简单的事情.就像某某人说过,每个人都有弱点和优点,我到目前为止,只是初步的 ...

随机推荐

  1. SharePoint : 使用SPQuery对象时要注意的事项

    转:http://www.cnblogs.com/chenxizhang/archive/2009/10/23/1588415.html 我们经常需要对一个列表进行查询,此时最灵活的方式就是直接使用S ...

  2. android WebViewClient的方法解释

    1.在点击请求的是链接是才会调用,重写此方法返回true表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边. public boolean shouldOverrideUrlLo ...

  3. Spring MVC Controller配置方式

    第一种 URL对应Bean如果要使用此类配置方式,需要在XML中做如下样式配置 以上配置,访问/hello.do就会寻找ID为/hello.do的Bean,此类方式仅适用小型的应用系统 第二种 为UR ...

  4. BZOJ2818: Gcd 欧拉函数求前缀和

    给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...

  5. jps 显示process information unavailable解决方法

    jps 显示process information unavailable解决办法jps时出现如下信息: 4791 -- process information unavailable 解决办法: 进 ...

  6. linux内存分配机制

    这几天在观察apache使用内存情况,所以特意了解了下linux的内存机制,发现一篇写得还不错.转来看看. 一般来说在ps aux中看到的rss就是进程所占用的物理内存.但是如果将所有程序的rss加起 ...

  7. Java和C++的区别

    这是一个Java语言和C++语言之间的比较. 目录 [隐藏]  1 设计目标 2 语言特性 2.1 语法 2.2 语义 2.3 资源管理 2.4 库 2.5 运行时 2.6 模板 vs. 泛型 2.7 ...

  8. maven,spring,mybatis集成错误

    maven,spring,mybatis集成的时候单元测试junit测试没问题,但mvn jetty:run 就报错误 错误: org.apache.ibatis.binding.BindingExc ...

  9. 【Java基础】用LinkedList实现一个简单栈的功能

    栈的基本功能 栈的最基本功能是保障后进先出,然后在此基础上可以对在栈中的对象进行弹入弹出,此外,在弹出时,如果栈为空,则会报错,所以还需要提供获取当前栈大小的方法. 构造存储对象Student /** ...

  10. Java 常见异常及趣味解释

    java.lang ArithmeticException 你正在试图使用电脑解决一个自己解决不了的数学问题,请重新阅读你的算术表达式并再次尝试. ArrayIndexOutOfBoundsExcep ...