简体   繁体   English

类继承

[英]Class Inheritance

I am trying to get completely to grips with class inheritence in Python. 我试图完全掌握Python中的类继承。 I have created program's with classes but they are all in one file. 我用类创建了程序,但是它们都在一个文件中。 I have also created scripts with multiple files containing just functions. 我还创建了包含多个仅包含函数的文件的脚本。 I have started using class inheritence in scripts with multiple files and I am hitting problems. 我已经开始在具有多个文件的脚本中使用类继承,但是遇到了问题。 I have 2 basic scripts below and I am trying to get the second script to inherit values from the first script. 我下面有2个基本脚本,并且我正在尝试获取第二个脚本以从第一个脚本继承值。 The code is as follow's: 代码如下:

First Script: 第一个脚本:

class test():

    def q():

        a = 20
        return a

    def w():
        b = 30
        return b

    if __name__ == '__main__':
        a = q()
        b = w()

if __name__ == '__main__':
    (a, b) = test()

Second Script: 第二个脚本:

from class1 import test

class test2(test):

    def e(a, b):
        print a
        print b


    e(a, b)

if __name__ == '__main__':
    test2(test)

Can anyone explain to me how to get the second file to inherit the first files values? 谁能向我解释如何获取第二个文件来继承第一个文件的值? Thanks for any help. 谢谢你的帮助。

I would say you messed up class definition with function stuff. 我会说你把函数定义弄乱了类定义。 It should look more like this: 它看起来应该像这样:

class Test(object):

    def __init__(self):
        self.a = 20
        self.b = 30

if __name__ == '__main__':
    test_instance = Test()

and

from class1 import Test

class Test2(Test):

    def e(self):
        print self.a
        print self.b


if __name__ == '__main__':
    test_instance = Test2()
    test_instance.e() # prints 20 and 30

It looks like your problem is not (only) inheritance, but also how to correctly define classes in Python . 看来您的问题不单单是继承,还在于如何正确定义Python中的类

Some notes: 一些注意事项:

  • Always use capitalized names for classes. 始终对类使用大写名称。 That is more or less convention. 那或多或少是惯例。
  • As ruibm pointed out, every (non-static) method of a class has to have a first parameter that is named (by convention) self . 正如ruibm所指出的,类的每个(非静态)方法都必须具有第一个参数(按惯例)名为self
  • You can create instance variables by setting them as self.variable = value in the __init__ method. 您可以通过在__init__方法self.variable = value实例变量设置为self.variable = value来创建实例变量。
  • If you call Test() you get an object back. 如果调用Test()则会返回一个对象。 Unless you assign it to a variable, just calling test2() as you did in your second piece of code has no effect. 除非将其分配给变量, test2()像在第二段代码中那样仅调用test2()无效。 Maybe it had in your case because defined your class in a weird way. 也许对您而言,是因为您以一种怪异的方式定义了您的班级。

In Python, each member function (method) of a class should have a variable called self which is pretty much the this pointer/reference in C++, Java, C#. 在Python中,类的每个成员函数(方法)都应具有一个称为self的变量,该变量几乎是C ++,Java,C#中的this指针/引用。

Basically, to make your code work add self as the first argument to all methods. 基本上,为了使您的代码正常工作,请将self作为所有方法的第一个参数。 To assign/read member variables use self.a and self.b otherwise you're just creating temporary function variables the way you have it right now. 要分配/读取成员变量,请使用self.aself.b否则您self.b现在的方式创建临时函数变量。

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

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