Creating an Instance of a Class with a variable in Python

后端 未结 8 1288
野性不改
野性不改 2020-12-29 05:23

I\'m trying to create a game for my little sister. It is a Virtual Pet sort of thing and the Pet has toys to play with.

I created a class Toy and want t

8条回答
  •  太阳男子
    2020-12-29 05:36

    Let's say you have three classes: Enemy1, Enemy2, Enemy3. This is how you instantiate them directly:

    Enemy1()
    Enemy2()
    Enemy3()
    

    but this will also work:

    x = Enemy1
    x()
    x = Enemy2
    x()
    x = Enemy3
    x()
    

    Is this what you meant?

提交回复
热议问题