gpt4 book ai didi

swift - 函数和返回值

转载 作者:搜寻专家 更新时间:2023-10-30 22:30:02 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com