作者:fengsh998
原文地址:http://blog.csdn.net/fengsh998/article/details/29606137
转载请注明出处
如果觉得文章对你有所帮助,请通过留言或关注微信公众帐号fengsh998来支持我,谢谢!
swift扩展了很多功能和属性,有些也比较奇P。只有慢慢学习,通过经验慢慢总结了。
下面将初步学习一下类的写法。
码工,最大爱好就是看码,而不是文字,太枯燥。
-
-
- //swiftDemo
- //
- //Createdbyappleon14-6-8.
- //Copyright(c)2014年fengsh.Allrightsreserved.
- /*
- 写本例子的目的在于快速学习swift类的写法,包括知识点:
- 1.属性设置
- 2.构造、释构
- 3.接口实现多态
- 4.函数的重载(重载很特别不需要overload关键词Delphi的朋友注意了)和重写(override)
- 5.类函数(静态成员函数)
- 6.各种函数的声明,带参,默认值,多个返回,多个输出参数,多个未确定参数的函数,内连函数等
- 7.函数类型变量,函数地址作为传参,返回函数地址(还未完工,学习中)
- 8.单例
- 9.swift新功能willset,didset@lazy属性
- 10.(后续学习补充)
- */
-
- importFoundation
- varinstance:Computer?
- letunk="unKnow"
-
- //显示器屏幕宽高
- structMonitorWH{
- varwidth=0
- varheight=0
- varresolution=0.0
- }
- //协义,接口,实现多重继承
- protocolProtocolComputer{
- varprice:Double{get}
- funcrunComputer()
- }
- //计算机类型
- enumComputerType:Int
- {
- casenone
- casebook
- casesuperBook
- casehome
- funccallbackWhenStarting()
- {
- //计算机类
- classComputer:NSObject,ProtocolComputer
- varcpu=unk
- varmemory=unk
- varhardDisk=unk
- varmonitor=unk
- varcpName=unk
- varcomputertype:ComputerType=.none
- //@lazy//这关键词声明的有啥作用啊????
- //继承接口的属性
- varprice:Double=0.0
- //willsetdidset属性
- vartotalPrice:Int=0{
- willSet(newTotalPrice){
- println("准备将totalPrice值(原值为:(totalPrice))设为:(newTotalPrice)")
- //todosomthingbeforeset.
- didSet{
- iftotalPrice>oldValue{
- println("设置后新值比旧值增加了(totalPrice-oldValue)")
- //声明一个set,get属性
- varcomputerPrice:Double{
- get{
- println("youcallcomputerPrice.")
- returnprice
- set{
- price=newValue
- println("yousetcomputerPricevalueis(price)")
- //默认构造
- init()
- println("defaultcreatrustoriscalled.")
- //默认构造不能和init()共存
- //convenienceinit(){
- //self.init(computerName:"unknow",price:0)
- //}
- //自定义构造函数
- init(computerName:String,price:Double)
- println("customcreatrustoriscalled.")
- self.cpName=computerName
- self.price=price
- //释构
- deinit{
- println("thisisdestory?")
- funcdescription()->String
- //还真不知道怎么换行来写代码以前可以使用现在被作参用了
- return"Computerdescription:product(self.cpName),typeis(self.computertype.toRaw()),cpuis(self.cpu),memoryis(self.memory),diskis(self.hardDisk),monitoris(self.monitor),priceis(self.price)"
- //类函数(OC中的+号操作,c/c++中的static函数)
- classfuncshareInstance()->Computer
- returnComputer()
- //开机关机(不带返回值函数)
- funcoperationComputer(onOrOff:Bool)
- ifonOrOff
- println("computerisstarting")
- else
- println("computerisstopping")
- //无参,无返回值函数
- funccomputerRunning()
- println("computerisrunning")
- //多个返回值(即输出参数)
- funcgetComputerConfig()->(cpu:String,hd:String,mem:String,mon:String)
- return(self.cpu,self.hardDisk,self.memory,self.monitor)
- //使用inout参数来作为输出参数
- funcgetComputerConfig(inoutcpu:String,inouthd:String,inoutmem:String,inoutmon:String)
- cpu=self.cpu
- hd=self.hardDisk
- mem=self.memory
- mon=self.monitor
- //外部参数名函数(目的是让调用者更加清楚每个参数的具体函义)
- //computerCPU,withComputerhardDisk,withComputerMemory,withComputerMonitor这些都是外部参数名
- //在调用时必须带上
- funcsetComputerConfig(computerCPUcpu:String,withComputerhardDiskhd:String,
- withComputerMemorymem:String,withComputerMonitormon:String)
- self.cpu=cpu
- self.hardDisk=hd
- self.memory=mem
- self.monitor=mon
- //使用#来把变量名提升了具有外部参数名作用的变量名,这样就不用再写一次外部参数名(在外部参数名与变量名相同时使用)
- funcsetComputerConfig(#cpu:String,disk:String,mon:String)
- self.cpu=cpu
- self.hardDisk=disk
- self.memory=mem
- self.monitor=mon
- //参数的默认值
- funcmacBookPro(pname:String="Apple",cpu:String="IntelCoreI5",type:ComputerType,
- mem:String="2G",mon:String="IntelHDGraphics4000")
- self.cpName=pname
- self.computertype=type
- //可变参数
- funcusbNumbers(usbs:String...)->String
- varret:String=""
- forusbinusbs
- println(usb)
- ret+=(usb+",")
- returnret
- //常量参数、变量参数
- //尽管函数内部修改了version但并不影响原来外部设定的值
- funclookWindowsVersion(varversion:String)->String
- version="defaultwindows"+version
- returnversion
- //mutatingfunc
- funcgetResolution(pname:String)->MonitorWH
- varmt=MonitorWH(width:1364,height:1280,resolution:16/9)
- ifpname=="Phripse"
- mt=MonitorWH(width:5555,height:3333,resolution:29/10)
- returnmt
- //函数作为参数传参
- //varcallbackWhenStarting:()->()=callbackWhenStarting
- //函数作为返回值
- //函数作为变量定义
- //嵌套函数
- funcopenTask()
- funcopenOtherTask()
- println("openothertask")
- println("opentask")
- //函数重写
- funclookComputerBasicHardInfo(computer:Computer)
- //接口实现
- funcrunComputer()
- println("Computerrun.")
- classLenove:Computer
- overridefunclookComputerBasicHardInfo(computer:Computer)
- ifcomputerisLenove
- println("这是联想")
- }
调用DEMO:
//varcpt=Computer()//调用默认构造
- varcpt=Computer(computerName:"Apple",price:12000)
- println(cpt.description)
- println(cpt.getComputerConfig())
- //属性测试
- println("价钱为:(cpt.computerPrice)")
- cpt.computerPrice=2000.0;
- println("设置后的价钱为:(cpt.computerPrice)")
- //测试willsetdidset
- cpt.totalPrice=100;
- cpt.totalPrice=400;
- cpt.totalPrice=900;
- vara="",b="",c="",d=""
- cpt.getComputerConfig(&a,hd:&b,mem:&c,mon:&d)
- println("a=(a),b=(b),c=(c),d=(d)")
- cpt.setComputerConfig(computerCPU:"interi5",withComputerhardDisk:"WD500",
- withComputerMemory:"4G",withComputerMonitor:"Phripse")
- println("最新配置:(cpt.description)")
- cpt.setComputerConfig(cpu:"AMD",disk:"HD1T",mem:"8G",mon:"SamSung")
- println("最新配置:(cpt.description)")
- //使用缺省值调用函数
- cpt.macBookPro(type:ComputerType.book,disk:"5")
- println("平果配置:(cpt.description)")
- letusbSupportType=cpt.usbNumbers("2.0","3.0")
- println("支持USB接口:(usbSupportType))")
- letextentUsbType=cpt.usbNumbers("5.0")
- println("扩展USB接口:(extentUsbType)")
- varversion="xp3";
- letnewversion=cpt.lookWindowsVersion(version);
- println(version)
- println(newversion)
输出:
customcreatrustoriscalled.
- Computerdescription:productApple,typeis0,cpuisunKnow,memoryisunKnow,diskisunKnow,monitorisunKnow,priceis12000.0
- (unKnow,unKnow,unKnow)
- youcallcomputerPrice.
- 价钱为:12000.0
- yousetcomputerPricevalueis2000.0
- youcallcomputerPrice.
- 设置后的价钱为:2000.0
- 准备将totalPrice值(原值为:0)设为:100
- 设置后新值比旧值增加了100
- 准备将totalPrice值(原值为:100)设为:400
- 设置后新值比旧值增加了300
- 准备将totalPrice值(原值为:400)设为:900
- 设置后新值比旧值增加了500
- a=unKnow,b=unKnow,c=unKnow,d=unKnow
- 最新配置:Computerdescription:productApple,cpuisinteri5,memoryis4G,diskisWD500,monitorisPhripse,priceis2000.0
- 最新配置:Computerdescription:productApple,cpuisAMD,memoryis8G,diskisHD1T,monitorisSamSung,priceis2000.0
- 平果配置:Computerdescription:productApple,typeis1,cpuisIntelCoreI5,memoryis2G,diskis5,monitorisIntelHDGraphics4000,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> 2.0
- 3.0
- 支持USB接口:2.0,3.0,)
- 5.0
- 扩展USB接口:5.0,
- xp3
- defaultwindowsxp3
- thisisdestory?
样子最好自己写一个从过种中去学习。光看,也许还不清楚是什么。
谢谢大家,因为是英文文档,看得我头也比较痛,有些要猜和运行来理解,还有些没有完善有点乱。有些没有搞懂所以就没有整理好。
大家共同学习,共同进步。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|