Swift相当新:无法让我的班级互动

时间:2018-10-09 02:58:28

标签: swift

我有此代码:

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是“使用未声明的类型”类名”。

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

1 个答案:

答案 0 :(得分:1)

这有效。尝试将类定义移到其他位置。

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"