Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:从子类访问父属性_Python_Python 3.x - Fatal编程技术网

Python:从子类访问父属性

Python:从子类访问父属性,python,python-3.x,Python,Python 3.x,在Python中,我有以下代码作为一个问答题: class Big_Cat: def __init__(self): self.x = "dangerous" class Cat(Big_Cat): def __init__(self): self.y = "quiet" new_cat = Cat() print(new_cat.x, new_cat.y) 因为cat类是从BigCat类继承的,所以它

在Python中,我有以下代码作为一个问答题:

class Big_Cat:
    def __init__(self):
        self.x = "dangerous"

class Cat(Big_Cat):
    def __init__(self):
        self.y = "quiet"

new_cat = Cat()
print(new_cat.x, new_cat.y)
因为cat类是从BigCat类继承的,所以它也应该可以访问变量x。那么为什么它会在打印屏幕行上抛出错误呢。new_cat如何从父类访问变量x?

您可以使用super调用父类'\uu init'__

您可以使用super调用父类'\uuu init'__


从超类继承后,必须调用父类的_init__构造函数。可以使用获取对父类的引用

以下是一个例子:

class Big_Cat:
    def __init__(self):
        self.x = "dangerous"

class Cat(Big_Cat):
    def __init__(self):
        super().__init__()
        self.y = "quiet"

new_cat = Cat()
print(new_cat.x, new_cat.y)
输出:


从超类继承后,必须调用父类的_init__构造函数。可以使用获取对父类的引用

以下是一个例子:

class Big_Cat:
    def __init__(self):
        self.x = "dangerous"

class Cat(Big_Cat):
    def __init__(self):
        super().__init__()
        self.y = "quiet"

new_cat = Cat()
print(new_cat.x, new_cat.y)
输出:


您需要在子类的构造函数中调用父类的构造函数,以便子类访问父类的方法和属性。你可以借助超级方法来实现

class Big_Cat:
    def __init__(self):
        self.x = "dangerous"

class Cat(Big_Cat):
    def __init__(self):
        super().__init__()
        self.y = "quiet"
        
new_cat = Cat()
print(new_cat.x, new_cat.y)

您需要在子类的构造函数中调用父类的构造函数,以便子类访问父类的方法和属性。你可以借助超级方法来实现

class Big_Cat:
    def __init__(self):
        self.x = "dangerous"

class Cat(Big_Cat):
    def __init__(self):
        super().__init__()
        self.y = "quiet"
        
new_cat = Cat()
print(new_cat.x, new_cat.y)

Python中的方法与C++或java语言中的OOP语言有不同的方法。 在类定义中不存在直接声明属性以使该属性自动成为实例属性的情况:

an_属性是类A的属性,但不是此类实例的属性:

a = A()                     # an instance of the class A
print(a.an_attribute)       # 0 - so IS the an_attribute an instance's attribute?
看起来_属性是实例的属性,但是

A.an_attribute = 100        # changing the value of a CLASS attribute
print(a.an_attribute)       # 100; so it is NOT the independent OBJECT 's attribute
那么如何创建对象的属性呢?非常简单:

a.an_attribute = 200        # creating an OBJECT's attribute

print(a.an_attribute)       # 200 — the OBJECT's attribute, independent of a CLASS' one
print(A.an_attribute)       # 100 — the CLASS attribute
从这一刻起,对象a有自己的属性,不同于同名的class属性

这意味着同一类的不同实例可能不仅具有相同属性的不同值,甚至具有完全不同的属性:

非常奇怪的情况:

对象a具有属性a_属性,但同一类的对象b没有, 对象b具有不同的_属性,但对象a没有。 有没有办法在类定义中规定/初始化实例的属性

幸运的是,有一个特殊的方法uuu init uuuuu,当您创建一个类的实例时,它会自动运行,并自动接收刚刚创建的对象作为它的第一个参数,通常称为this

因此,您可以使用此自动填充参数为刚创建的对象指定一个属性:

class A:
    def __init__(self):
        self.an_attribute = 20
        self.different_attribute = 50
现在,类A的所有新实例都将有自己的、对象的属性、一个_属性和不同的_属性,分别用值20和50初始化,这在这里并不重要


因此,实例变量不会自动被子类继承。其他人已经解释过,如何在java中使用它,在超级C++函数的子类中,不必惊讶。在Python中,

< P>与真正的OOP语言有不同的方法,如C++或Java。 在类定义中不存在直接声明属性以使该属性自动成为实例属性的情况:

an_属性是类A的属性,但不是此类实例的属性:

a = A()                     # an instance of the class A
print(a.an_attribute)       # 0 - so IS the an_attribute an instance's attribute?
看起来_属性是实例的属性,但是

A.an_attribute = 100        # changing the value of a CLASS attribute
print(a.an_attribute)       # 100; so it is NOT the independent OBJECT 's attribute
那么如何创建对象的属性呢?非常简单:

a.an_attribute = 200        # creating an OBJECT's attribute

print(a.an_attribute)       # 200 — the OBJECT's attribute, independent of a CLASS' one
print(A.an_attribute)       # 100 — the CLASS attribute
从这一刻起,对象a有自己的属性,不同于同名的class属性

这意味着同一类的不同实例可能不仅具有相同属性的不同值,甚至具有完全不同的属性:

非常奇怪的情况:

对象a具有属性a_属性,但同一类的对象b没有, 对象b具有不同的_属性,但对象a没有。 有没有办法在类定义中规定/初始化实例的属性

幸运的是,有一个特殊的方法uuu init uuuuu,当您创建一个类的实例时,它会自动运行,并自动接收刚刚创建的对象作为它的第一个参数,通常称为this

因此,您可以使用此自动填充参数为刚创建的对象指定一个属性:

class A:
    def __init__(self):
        self.an_attribute = 20
        self.different_attribute = 50
现在,类A的所有新实例都将有自己的、对象的属性、一个_属性和不同的_属性,分别用值20和50初始化,这在这里并不重要


因此,实例变量不会自动被子类继承。其他人已经解释了如何绕过它——毫不奇怪,在超级内置函数的帮助下,在子类的_uinit_u方法中。

您没有调用超类的init方法,因此没有指定x。Yo u没有调用超类的init方法,因此没有指定x。