gpt4 book ai didi

Swift 类和函数

转载 作者:行者123 更新时间:2023-11-30 12:41:57 26 4
gpt4 key购买 nike

我正在学习 swift 类(class),我必须为此场景编写代码,但我不知道为什么我的函数不起作用。我感到非常恼火。

//: Playground - noun: a place where people can play

import Cocoa

// create class

class State
{
var population: Double?
var capital: String?
var name: String?
var growthRate: Double?
var nextYearPopulation: Double?
var neighbors: [State]


// initialize vars
init(population: Double?, capital: String?, name: String?, growthRate: Double?, neighbors: [State])
{
self.population = population
self.capital = capital
self.name = name
self.growthRate = growthRate
self.neighbors = neighbors
}

var Texas = State(population: 1000000, capital: "Austin", name: "Texas", growthRate: 0.012, neighbors: [])

var Oklahoma = State(population: 3000000, capital: "Oklahoma City", name: "Oklahoma", growthRate: 0.01, neighbors: [])

var NewMexico = State(population: 1000000, capital: "Santa Fe", name: "New Mexico", growthRate: 0.02, neighbors: [])

var NewYork = State(population: 25000000, capital: "New York City", name: "New York", growthRate: 0.1, neighbors: [])

var Virginia = State(population: 15000000, capital: "Richmond", name: "Virginia", growthRate: 0.03, neighbors: [])

var Florida = State(population: 20000000, capital: "Tallahassee", name: "Florida", growthRate: 0.07, neighbors: [])

//functions

func populationAfter()
{
nextYearPopulation = self.growthRate! * self.population!
print("The projected population for ", self.name, "is ", nextYearPopulation)
}

Texas.populationAfter()


}

最佳答案

您需要将状态var移动到类定义之后并调用Texas.populationAfter()。对于 State 来说,拥有名为 TexasOklahoma 等的属性是没有意义的,但这就是您的代码正在做的事情。在代码中调用 Texas.populationAfter() 是完全非法的,因为代码需要位于函数声明内。

通过将状态的创建移到 State 类之外,您可以使用 State 类定义创建变量,然后您可以调用 名为 TexasState 实例上的populationAfter() 方法。

请注意,在 Swift 中,建议对变量和属性使用以小写字母开头的字符串,对类和结构类型使用以大写字母开头的字符串。

请注意,您的 populationAfter 函数只是计算人口增长。要获得新的更新人口,请乘以 1 + self.growthRate!

class State
{
var population: Double?
var capital: String?
var name: String?
var growthRate: Double?
var nextYearPopulation: Double?
var neighbors: [State]

// initialize vars
init(population: Double?, capital: String?, name: String?, growthRate: Double?, neighbors: [State])
{
self.population = population
self.capital = capital
self.name = name
self.growthRate = growthRate
self.neighbors = neighbors
}

//functions

func populationAfter()
{
nextYearPopulation = (1 + self.growthRate!) * self.population!
print("The projected population for ", self.name!, "is ", nextYearPopulation!)
}
}

var Texas = State(population: 1000000, capital: "Austin", name: "Texas", growthRate: 0.012, neighbors: [])

var Oklahoma = State(population: 3000000, capital: "Oklahoma City", name: "Oklahoma", growthRate: 0.01, neighbors: [])

var NewMexico = State(population: 1000000, capital: "Santa Fe", name: "New Mexico", growthRate: 0.02, neighbors: [])

var NewYork = State(population: 25000000, capital: "New York City", name: "New York", growthRate: 0.1, neighbors: [])

var Virginia = State(population: 15000000, capital: "Richmond", name: "Virginia", growthRate: 0.03, neighbors: [])

var Florida = State(population: 20000000, capital: "Tallahassee", name: "Florida", growthRate: 0.07, neighbors: [])

Texas.populationAfter()

关于Swift 类和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42126617/

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