我正在学习swift课程,我不得不为这个senario写一个代码,我不知道为什么我的功能不起作用。我非常恼火。
//: 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()
}
答案 0 :(得分:0)
您需要移动州var
并在课程定义后致电Texas.populationAfter()
。 State
具有Texas
,Oklahoma
等属性是没有意义的,但这就是您的代码所做的事情。在代码中调用Texas.populationAfter()
是完全非法的,因为代码需要在函数声明中。
通过在State
类之外创建状态,然后使用State
类定义创建变量,然后可以在populationAfter()
上调用State
方法{1}}名为Texas
的实例。
请注意,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()