- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Swift 的初学者,所以有些事情我还不是很清楚。我希望有人能给我解释一下:
// Creating Type Properties and Type Methods
class BankAccount {
// stored properties
let accountNumber: Int
let routingCode = 12345678
var balance: Double
class var interestRate: Float {
return 2.0
}
init(num: Int, initialBalance: Double) {
accountNumber = num
balance = initialBalance
}
func deposit(amount: Double) {
balance += amount
}
func withdraw(amount: Double) -> Bool {
if balance > amount {
balance -= amount
return true
} else {
println("Insufficient funds")
return false
}
}
class func example() {
// Type methods CANNOT access instance data
println("Interest rate is \(interestRate)")
}
}
var firstAccount = BankAccount(num: 11221122, initialBalance: 1000.0)
var secondAccount = BankAccount(num: 22113322, initialBalance: 4543.54)
BankAccount.interestRate
firstAccount.deposit(520)
这是代码。我想知道为什么 deposit()
没有返回箭头和 return 关键字而 withdraw()
有。什么时候用返回箭头,在什么情况下,有什么规定吗?我不明白。
另外...每个人都对您的回答非常友善,现在我越来越清楚了。
教程开头有函数练习代码
// Function that return values
func myFunction() -> String {
return “Hello”
}
我想这里不需要这个返回值,但在教程中他们想向我们展示它存在,我说得对吗?
此外,我是否可以犯一个“错误”并以某种方式在我的存款功能中使用返回箭头和值?我试过这个:
func deposit(amount : Double) -> Double {
return balance += amount
}
...但是它产生了错误。
我在上一家公司看到了高级编码,他们正在创建具有许多自定义和很酷功能的在线商店,并且所有代码都充满了返回箭头。这让我感到困惑,我认为这是在 OOP 中创建方法/函数的默认设置。
附加问题!我想玩函数所以我想创建一个函数 transferFunds() 将钱从一个帐户转移到另一个帐户。我做了这样的功能
func transferFunds(firstAcc : Int, secondAcc : Int, funds : Double) {
// magic part
if firstAcc == firstAccount.accountNumber {
firstAccount.balance -= funds
} else {
println("Invalid account number! Try again.")
}
if secondAcc == secondAccount.accountNumber {
secondAccount.balance += funds
} else {
println("Invalid account number! Try again.")
}
}
这是我想到的一个简单代码,但我知道它甚至可能很愚蠢。我知道应该有一个代码来检查我从中取钱的第一个帐户中是否有足够的资金,但是好吧......让我们玩这个。我想在函数 transferFunds()
的参数中指定 accountNumbers
或其他内容,我想搜索我想象中的银行中使用类 BankAccount 的所有对象/客户端
以便找到一个然后转账。我不知道我是否正确描述了我的问题,但我希望你明白我想做什么。有人可以帮帮我吗?
最佳答案
所以在 Swift 中,没有箭头 的函数的返回类型为 Void
:
func funcWithNoReturnType() {
//I don't return anything, but I still can return to jump out of the function
}
这可以重写为:
func funcWithNoReturnType() -> Void {
//I don't return anything, but I still can return to jump out of the function
}
所以在你的情况下......
func deposit(amount : Double) {
balance += amount
}
你的方法 deposit 接受一个参数 Type Double 并且这个返回 什么都没有,这正是您在方法声明中看不到 return 语句的原因。这种方法只是简单地向您的帐户添加或存入更多资金,不需要返回单。
但是,在您的 withdraw 方法上:
func withdraw(amount : Double) -> Bool {
if balance > amount {
balance -= amount
return true
} else {
println("Insufficient funds")
return false
}
}
此方法接受一个 Double 类型 的参数,并返回一个 bool 值。关于您的取款方法,如果您的余额少于您尝试取款的金额(金额),那么这是不可能的,这就是为什么它返回 false,但如果您的帐户中确实有足够的钱,它会优雅地 < em>取出钱,返回true,就好像操作成功了一样。
我希望这能稍微澄清您的困惑。
关于swift - 函数和返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31008846/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!