Swift_方法
Swift_方法
///方法
class Methods: NSObject {
func test() {
// self.testInstanceMethods() //实例方法
self.testTypeMethods() //类型方法
}
//实例方法
func testInstanceMethods() {
self.testLocalAndExternalParameterNames()
self.testSelfProperty()
self.testModifyingValueTypesFromWithinInstanceMethods()
self.testAssigningToSelfWithinMutatingMethod()
}
//内部和外部属性
func testLocalAndExternalParameterNames() {
class Counter {
var count: Int = 0
func incrementBy(_ amount: Int, numberOfTimes: Int) {
count += amount * numberOfTimes
}
}
let counter = Counter()
counter.incrementBy(5, numberOfTimes: 3)
print("\(counter.count)")
15
*/
}
//self属性
func testSelfProperty() {
//self属性
struct Point {
var x = 0.0, y = 0.0
func isToTheRightOfX(_ x: Double) -> Bool {
//这里有内部和外部属性
return self.x > x
}
}
let somePoint = Point(x: 4.0, y: 5.0)
print("\(somePoint.isToTheRightOfX(1.0))")
/*
true
*/
}
//在实例方法中修改值类型
func testModifyingValueTypesFromWithinInstanceMethods() {
//因为结构体是值对象,其内部方法无法修改外部值,为了让结构体支持修改结构体内的属性。
//方法体前加mutating,让结构体的实例方法可以修改结构体中的值
struct Point {
var x = 0.0, y = 0.0
mutating func moveByX(_ deltaX: Double, y deltaY: Double) {
x += deltaX
y += deltaY
}
}
var somePoint = Point(x: 1.0, y: 1.0)
somePoint.moveByX(2.0, y: 3.0)
print("(\(somePoint.x), \(somePoint.y))")
let fixedPoint = Point(x: 3.0, y: 3.0)
print(fixedPoint)
// 结构体是值对象,使用let常量后,无法修改内部值
// fixedPoint.moveByX(2.0, y: 3.0) // 抛错
(3.0, 4.0)
(Point #1)(x: 3.0, y: 3.0)
*/
}
//自我变异
func testAssigningToSelfWithinMutatingMethod() {
//mutating还可以修改当前结构体和当前枚举
//结构体测试
struct Point {
var x = 0.0, y = 0.0
mutating func moveByX(_ deltaX: Double, y deltaY: Double) {
self = Point(x: x + deltaX, y: y + deltaY)
}
}
var point = Point()
point.moveByX(1.0, y: 1.0)
print(point)
//枚举测试
enum TriStateSwitch {
case off, low, high
mutating func next() {
switch self {
case TriStateSwitch.off:
self = TriStateSwitch.low
print(self)
case TriStateSwitch.low:
self = TriStateSwitch.high
print(self)
case TriStateSwitch.high:
self = TriStateSwitch.off
print(self)
}
}
}
var ovenLight = TriStateSwitch.low
ovenLight.next()
ovenLight.next()
/*
(Point #1)(x: 1.0, y: 1.0)
high
off
*/
}
//类型方法
func testTypeMethods() {
//类
class SomeClass {
class func someTypeMethod() {
// type method implementation goes here
}
}
SomeClass.someTypeMethod()
// 结构体
struct LevelTracker {
// static修改属性,方法体要修改static属性,方法前要使用static
static var highestUnlockedLevel = 1
static func levelIsUnlocked(_ level: Int) -> Bool {
return level <= highestUnlockedLevel
}
}
print("\(LevelTracker.levelIsUnlocked(2))")
/*
false
*/
}
}
Swift_方法的更多相关文章
- javaSE27天复习总结
JAVA学习总结 2 第一天 2 1:计算机概述(了解) 2 (1)计算机 2 (2)计算机硬件 2 (3)计算机软件 2 (4)软件开发(理解) 2 (5) ...
- Swift_协议
Swift_协议 点击查看源码 //协议 @objc protocol SomeProtocol:class { //class代表只用类才能实现这个协议 func test() //@objc:OC ...
- Swift_销毁
Swift_销毁 点击查看源码 销毁 func test() { class SomeClass { //类销毁时 通知此方法 deinit { print("销毁") } } v ...
- Swift_初始化
#Swift_初始化 点击查看源码 初始化结构体 //初始化结构体 func testInitStruct() { //结构体 类中默认方法 struct Size { //宽 var width = ...
- Swift_继承
Swift_继承 点击查看源码 func testInheritance() { //基类 class Base { var count = 0.0 var description: String { ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- 【.net 深呼吸】细说CodeDom(6):方法参数
本文老周就给大伙伴们介绍一下方法参数代码的生成. 在开始之前,先补充一下上一篇烂文的内容.在上一篇文章中,老周检讨了 MemberAttributes 枚举的用法,老周此前误以为该枚举不能进行按位操作 ...
- IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法
直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
随机推荐
- 洛谷P3952 时间复杂度(模拟)
题意 题目链接 Sol 咕了一年的题解..就是个模拟吧 考场上写的递归也是醉了... 感觉一年自己进步了不少啊..面向数据编程的能力提高了不少 #include<bits/stdc++.h> ...
- 微信小程序request请求之GET跟POST的区别
1.GET 例子: wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { 'content ...
- 关于github改名问题
不喜欢github显示的目录名字于是百度了下,更改过程,记录下来,方便日后查看! 首页右上角点击出来菜单,找到Settings按钮点击 左侧找到Account账号菜单点击 找到change usern ...
- Mysql与web之间的数据、查询等个问题
Mysql与web之间的数据.查询等个问题 在自己写的一个jsp主页连接数据库出现的各种问题,写记下来与大家分享,共勉.最后附jdbc代码. ---DanlV Error 1---错误代码: java ...
- Hibernate (ORM)
1 框架体系结构 2 hibernate入门 2.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBean Relational: ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- SQL Server ->> Online Index Rebuilding(联机索引重建)
SQL Server的Enterprise Edition是支持联机索引重建的.那么联机索引重建是怎么工作的以及对我们的查询有什么影响呢? 既然是联机,SQL Server保持了现有索引对于用户的可用 ...
- Data Flow ->> Multiple Excel Sheet Loaded Into One Table
同个Excel文件中多个Sheet中的数据导入到单张表中,参考了文章:http://www.cnblogs.com/biwork/p/3478778.html 思路: 1) ForEach Loop组 ...
- U盘中毒了?教你如何删除System Volume Information这个顽固文件夹
不得不说cmd命令很好用呢.最近我的U盘中毒了,格式化都删除不了System Volume Information这个顽固的文件夹,真心伤不起哇!还好现在解决了问题.看来以后得好好对待U盘,不能乱用了 ...
- 在win7 windows 上编译 beego 上传到 linux 去执行
在beego的项目目录下,执行: GOOS=linux GOARCH=amd64 go build So easy!但是却搞了好久! 参考连接:http://blog.csdn.net/changji ...