简体   繁体   English

了解python类的继承

[英]Understanding inheritance with python classes

so I am trying to understand how to build child classes in python. 所以我试图了解如何在python中建立子类。

So ive created a perent and a child class but I just cant understand how to get them to work 所以我创建了一个perent和一个儿童班,但我只是不明白如何让他们工作

so this is my current code 所以这是我当前的代码

from abc import ABCMeta, abstractmethod

class Persion(object):
    __metaclass__ = ABCMeta # sets the metaclass to a abstract base class . that means we never call this class directley insted its used for child classes to inhearrit
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

    def talk(self):
        print("hi my name is " + self.name + " and I am a " + self.gender + ".")

    @abstractmethod
    def PersionType(self):
        """Returns a string of the childs type"""
        pass

class Player(Persion):
    def __int__(self,speed):
        self.name = name
        self.gender = gender
        self.speed = speed
        self.posX = 0
        self.posY = 0

    def moveXY(self, X, Y):
        self.posX = X
        self.posY = Y

    def PersionType(self):
        return 'Player'

player=Persion("Ben","m")
hero = Player(player,30)
hero.moveXY(20,20)
print("you are now at ", hero.posX, "," , hero.posY)
print("your speed is ", hero.speed)
print("your gender is ", hero.gender)
hero.talk()

so the end result is I want these functions to work. 所以最终结果是我希望这些功能能够正常工作。 but something has gone rong in buildign the inhertance thats what I want to find out. 但是在构建固有的东西时出现了错误,这就是我想发现的东西。

hero.moveXY(20,20)
print("you are now at ", hero.posX, "," , hero.posY)
print("your speed is ", hero.speed)
print("your gender is ", hero.gender)
hero.talk()

im getting errors sutch as 我收到错误提示

 line 36, in <module>
    print("your speed is ", hero.speed)
AttributeError: 'Player' object has no attribute 'speed'

I am making this to help me understand how inheratince works in python. 我这样做是为了帮助我了解inheratince如何在python中工作。

There are three main problems with your code: 您的代码存在三个主要问题

  1. Change __int__ to __init__ . __int__更改为__init__ Your instance isn't getting initialized, which is why you get the no attribute 'speed' error. 您的实例未初始化,这就是为什么您会收到no attribute 'speed'错误的原因。

  2. Your subclass (the class that's inheriting, which in this case is Player ) should have an __init__ method that takes all the arguments you want associated with it. 您的子类(正在继承的类,在这种情况下为Player )应具有__init__方法,该方法接受要与之关联的所有参数。 You can pass the ones used by the superclass ( Persion ) to the superclass's __init__ . 您可以Persion类( Persion )使用的那些传递给超类的__init__ That means probably changing the __init__ method to: 这意味着可能将__init__方法更改为:

     class Player(Persion): def __init__(self, name, gender, speed): super(Player, self).__init__(name, gender) self.speed = speed self.posX = 0 self.posY = 0 
  3. Now, when creating the instances, you need only create an instance of the subclass. 现在,在创建实例时,只需创建子类的实例。 It will inherit the methods from the superclass: 它将继承超类的方法:

     hero = Player('Ben', 'm', 30) 

With these changes, these lines: 通过这些更改,这些行:

print("you are now at ", hero.posX, "," , hero.posY)
print("your speed is ", hero.speed)
print("your gender is ", hero.gender)
hero.talk()

Now produce this output: 现在产生以下输出:

you are now at  20 , 20
your speed is  30
your gender is  m
hi my name is Ben and I am a m.

Additional small notes : 其他小注意事项

  • You probably want the class to be named Person instead of Persion . 您可能希望将类命名为Person而不是Persion
  • The @abstractmethod method should not be needed. @abstractmethod方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM