gpt4 book ai didi

swift - Swift : can't get my classes to interact 很新

转载 作者:行者123 更新时间:2023-11-28 14:09:08 24 4
gpt4 key购买 nike

我有这个代码:

class Lion
{
var name:String = "Default"
var isAlive : Bool = false

func eat(cow:Cow)
{
cow.isAlive = false
print("Cow \(cow) was eaten by \(name)")
}
}

var lion1 = Lion()
lion1.name = "Mufasa"
lion1.isAlive = true

var lion2 = Lion()
lion1.name = "Simba"
lion1.isAlive = true

var lion3 = Lion()
lion1.name = "Scar"
lion1.isAlive = true

class Cow
{
var name:String = "Default"
var isAlive : Bool = false

func giveMilk ()
{
print("cow \(name) is now giving milk")
}

func die ()
{
print("Cow \(name) is now dead")
}
}

var cow1 = Cow()
cow1.name = "Moo1"
cow1.isAlive = true

var cow2 = Cow()
cow2.name = "Moo2"
cow2.isAlive = true

var cow3 = Cow()
cow3.name = "Moo3"
cow3.isAlive = true

var cow4 = Cow()
cow4.name = "Moo4"
cow4.isAlive = true

var cow5 = Cow()
cow5.name = "Moo5"
cow5.isAlive = true

var cow6 = Cow()
cow6.name = "Moo6"
cow6.isAlive = true

var cow7 = Cow()
cow7.name = "Moo7"
cow7.isAlive = true

var cow8 = Cow()
cow8.name = "Moo8"
cow8.isAlive = true

var cow9 = Cow()
cow9.name = "Moo9"
cow9.isAlive = true

var cow10 = Cow()
cow10.name = "Moo10"
cow10.isAlive = true

class Cat
{
var name:String = "Default"

func eatMouse (mouse : Mouse)
{
print("Mouse \(mouse) was eaten")
}

func getMilk (cow:Cow)
{
if cow.isAlive == true
{
cow.giveMilk()
}else{
print("A dead cow cannot provide milk")
}
}
}

var cat1 = Cat()
cat1.name = "Meow1"

var cat2 = Cat()
cat2.name = "Meow2"

class Mouse
{
var name:String = "Default"

func die ()
{
print("Mouse \(name) is dead")
}
}

var mouse1 = Mouse()
mouse1.name = "Micky"

var mouse2 = Mouse()
mouse2.name = "Mini"

var mouse3 = Mouse()
mouse3.name = "Rattata"

var mouse4 = Mouse()
mouse4.name = "Splinter"

当我通过 Xcode 运行时,它告诉我 Cow、Mouse 和 Cat 是“使用未声明的类型“类名”。

对于我的生活,我无法弄清楚(很新,所以不要打击我)。建立类后,我应该让系统运行几个不同的场景,其中老鼠死了,猫吃老鼠,狮子吃牛等。

最佳答案

这行得通。尝试将类定义放在其他定义之前。

class Mouse
{
var name:String = "Default"

func die ()
{
print("Mouse \(name) is dead")
}
}

class Cow
{
var name:String = "Default"
var isAlive : Bool = false

func giveMilk ()
{
print("cow \(name) is now giving milk")
}

func die ()
{
print("Cow \(name) is now dead")
}
}

class Lion
{
var name:String = "Default"
var isAlive : Bool = false

func eat(cow:Cow)
{
cow.isAlive = false
print("Cow \(cow) was eaten by \(name)")
}
}

var lion1 = Lion()
lion1.name = "Mufasa"
lion1.isAlive = true

var lion2 = Lion()
lion1.name = "Simba"
lion1.isAlive = true

var lion3 = Lion()
lion1.name = "Scar"
lion1.isAlive = true



var cow1 = Cow()
cow1.name = "Moo1"
cow1.isAlive = true

var cow2 = Cow()
cow2.name = "Moo2"
cow2.isAlive = true

var cow3 = Cow()
cow3.name = "Moo3"
cow3.isAlive = true

var cow4 = Cow()
cow4.name = "Moo4"
cow4.isAlive = true

var cow5 = Cow()
cow5.name = "Moo5"
cow5.isAlive = true

var cow6 = Cow()
cow6.name = "Moo6"
cow6.isAlive = true

var cow7 = Cow()
cow7.name = "Moo7"
cow7.isAlive = true

var cow8 = Cow()
cow8.name = "Moo8"
cow8.isAlive = true

var cow9 = Cow()
cow9.name = "Moo9"
cow9.isAlive = true

var cow10 = Cow()
cow10.name = "Moo10"
cow10.isAlive = true

class Cat
{
var name:String = "Default"

func eatMouse (mouse : Mouse)
{
print("Mouse \(mouse) was eaten")
}

func getMilk (cow:Cow)
{
if cow.isAlive == true
{
cow.giveMilk()
}else{
print("A dead cow cannot provide milk")
}
}
}

var cat1 = Cat()
cat1.name = "Meow1"

var cat2 = Cat()
cat2.name = "Meow2"



var mouse1 = Mouse()
mouse1.name = "Micky"

var mouse2 = Mouse()
mouse2.name = "Mini"

var mouse3 = Mouse()
mouse3.name = "Rattata"

var mouse4 = Mouse()
mouse4.name = "Splinter"

关于swift - Swift : can't get my classes to interact 很新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712722/

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