A Swift Tour(4) - Objects and Classes
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的更多相关文章
- 【读书笔记】A Swift Tour
素材:A Swift Tour 推荐下载Playground:Download Playground objc 自己较为熟悉,想熟悉下风头正劲的 swift.就先从官方的入门手册开始撸. 每一小节,我 ...
- 冷市攻略:Listo 教你 25 今天的社会 Swift 语言 - 02 Swift Tour
import Foundation //******************************************************************************** ...
- [IOS]《A Swift Tour》翻译(一)
以下翻译内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3768936.html 碎碎念... Swift是苹果在WWDC刚发 ...
- Swift tour
输出函数: print(“hello world!") 无需引入函数库,无须使用“;”作为语句结尾,也无须写跟其它语言一样的main()函数,Swift中,全局区的代码就是程序入口.You ...
- A Swift Tour(3) - Functions and Closures
Functions and Closures 使用func来声明函数,通过括号参数列表的方式来调用函数,用 --> 来分割函数的返回类型,参数名和类型,例如: func greet(name: ...
- A swift Tour
传统的认为,一个新的语言的第一个应用程序都会打印"Hellow,Word",在Swift中,可以只需要一行代码: pringln("Hello, word") ...
- Swift学习——A Swift Tour 函数
Functions and Closures 函数和封闭性(闭包) Functions 函数的使用 Swift中的函数定义和OC中有明显的差别了,使用func定义函数,在括号里定义參数和类型,用 ...
- Swift学习——A Swift Tour 协议和扩展
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/28854395 Protocols an ...
- [Swift A] - A Swift Tour
首先说下自己对Swift语言的一点点看法,对于一个写过javascript和常年写java代码的人来说,学习Swift是一件很简单的事情.就像某某人说过,每个人都有弱点和优点,我到目前为止,只是初步的 ...
随机推荐
- js正则表达式及代码
//校验是否全由数字组成function isDigit(s){var patrn=/^[0-9]{1,20}$/;if (!patrn.exec(s)) return falsereturn tru ...
- 在Sharepoint2010中发布VS2010开发的WebPart
转:http://www.cnblogs.com/bfgl/archive/2012/03/22/2411698.html 本人接触Sharepoint2010到今天为止不到一个月.作为一名老C#程序 ...
- 实现简单的WebPart
转:http://www.cnblogs.com/gaoweipeng/archive/2009/10/26/1589269.html 在前面的文章中,我们讲解了很多基础的内容,主要包括安装配置.简单 ...
- Linux下的nginx启动、重新启动
nginx的启动命令是: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -c制定配置文件的路径,不加-nginx会自动 ...
- 【HTML】Intermediate3:Meta Tags
1.Meta tags were the town criers of the Internet Do anything to the content that is presented in the ...
- Hbase集群无法关闭
执行stop-hbase.sh关闭Hbase服务器,提示一直在等待,查阅了很多网上的资料找到了答案.因为hbase的主要信息存储在zookeeper集群中,zookeeper集群没有正常启动会导致hb ...
- List使用Foreach 修改集合时,会报错的解决方案 (Error: Collection was modified; enumeration operation may not execute. ) - 摘自网络
当用foreach遍历Collection时,如果对Collection有Add或者Remove操作时,会发生以下运行时错误: "Collection was modified; enume ...
- MVC client validation after PartialView loaded via Ajax MVC3中 弹出 Dialog时候 提交的时候 使用 Jquery 不验证 form表单 的解决办法
I came across this scenario whereby my main View uses Ajax posts to retrieve PartialViews and delive ...
- LLVM在静态分析上的增强 @ WWDC 2013
在代码还没有真正跑起来的时候,可以利用Clang对代码进行静态分析. 1. 可以应用快捷键Shift+Command+B对项目代码进行分析: 2. 也可以针对某个文件进行分析(现有版本貌似不能针对特定 ...
- opencv基础知识-videowriter
一.前言-简介 在试验中需要常常将实验结果进行保存,在opencv中提供很好用的录制视频的句柄,也可称之为类-videowriter. videowriter应用那是相当的简单,总之分为三步: //声 ...