cost 72 ms
mixin 中的方法取决于 mixin 到的 class 的方法时的情况 - Case when method in mixin depends on a method of the class that mixin-ed to

当 mixin 中的方法依赖于 class 的 mixin 方法时,我想问一下设计模式。 下面的例子是 python,但我相信其他语言也会出现这个问题。 例如,假设我有以下两个混入,我想注入一些 class。在下面的代码中,我想注入f但f要求 class 混入来实现g ,因为g将是用于ffrom a ...

C++,父母同祖的孩子 - C++, child with both parents having a same ancestor

我现在遇到 C++ 类和 inheritance 的问题... 假设我们有 每当我创建D时,它都会调用B的构造函数和C的构造函数,这会导致A的多个实例。 然后编译器说它不知道应该调用哪个“函数”,是来自B的那个还是来自C的那个。 我想知道如何只调用A构造函数一次,然后用它来构建B和C 。 我已经尝试 ...

了解 python 的多个 inheritance 中的 MRO、构造函数和方法 - understanding MRO, constructors, and methods in multiple inheritance of python

以下是两组代码:一组具有构造函数__init__() ,另一组具有display()方法。 代码一: 如果我们编译并运行上面的这组代码,output 将是: 现在,第二个代码块在下面, 如果在上面的第二个代码块中,我们将__init__()替换为display()方法,那么,由于class B中的 ...

将 **kwargs arguments 传递给多个 inheritance 中的父类会丢失 kwargs 内容 - Passing **kwargs arguments to parent classes in multiple inheritance loses kwargs content

在多个inheritance的场景下,Python3是如何将arguments传递给所有父类的? 考虑下面的玩具程序: output 是: 我期望做的是将相同的 kwargs 传递给所有父类,并让他们根据需要使用这些值。 但是,我看到的是,一旦我关闭了第一个父 class A ,kwargs 就被 ...

Python:重叠(非独占)继承以提供基于实例参数的方法 - Python: overlapping (non exclusive) inheritance to have methods available based on instance parameters

如果参数满足特定条件,我希望某些属性和方法仅在类实例中可用。 不同的情况不是唯一的。 我已经有了一个可行的解决方案(包括 ShadowRanger 的建议): 属性和方法的可用性取决于实例参数: 基类的属性和方法应该始终可用。 如果第一个参数等于 3,则子类 _triangle 的属性和方法应该可用 ...

如何组合python中不同父类定义的属性? - How to combine properties defined by different parent classes in python?

假设我们有两个类,每个类stuff以不同的方式计算属性。 是否可以将它们的输出组合到子类的方法/属性中? 下面的代码显示了预期的效果(尽管get_stuff_from需要用适当的 python 结构替换,如果存在的话)。class Foo(): @property def stuf ...

多个 class inheritance 一个祖父母,两个父母,一个孩子的类型错误 class - Multiple class inheritance TypeError with one grandparent, two parents, one child class

我正在练习 OOP 并不断遇到这个问题。 这是一个例子。 拿一个菱形倍数class inheritance排列,带Weapon feeding Edge和Long,都是双汉德继承的。 如果我在不继承 Weapon 的情况下对 Edge 进行编码,则代码可以正常工作。 但是一旦我将 Weapon 作 ...

使用多个时指针偏移不正确 inheritance - Pointer offset is incorrect when using multiple inheritance

我在访问成员变量时使用多个 inheritance 的某些代码中遇到错误。 不幸的是,我无法提供您可以运行的最小可重现示例,但我可以提供一堆关于我所看到的信息。 我的代码使用 GCC 编译为 ARM。 粗略地说,这就是我所拥有的(不是我的实际代码): 其中类 A、B 和 C 也从其他类多重继承。 ...

如何在 Python 多重继承中创建非覆盖方法存根? - How to make a non-overriding method stub in Python multi-inheritance?

假设您有 2 个混合类,每个类都定义了抽象方法和实现。 他们一起实现了每个方法,但根据 inheritance 的顺序,空存根将覆盖另一个 class 的实现。在大多数情况下至少有两种方法可以解决这个问题,但我不太喜欢这两种方法。 可以删除抽象方法,只依赖鸭子类型,但没有明确的接口定义和类型提示。 ...

为什么在使用 Inheritance 时会出现错误 - Why does this give me an error when using Inheritance

我目前正在学习 Inheritance。 下面的代码工作得很好。 但是,下面的代码不起作用。 错误我只在这两个代码片段之间更改了一件事,就是在 class Employee 旁边添加了“public Student”。 我做错了什么? 请使用简单的文字/逻辑来解释这一点。 ...

python 中的多个 inheritance 的 AttributeError 问题 - AttributeError Problem with Multiple inheritance in python

我想计算 python 中具有多重继承的学生的总分和平均分。 但是,每当我为我的孩子 class 创建一个 object 时,它让我可以访问父类的所有方法,但是当我调用孩子 class 的方法时会显示 AttributeError。 我尝试使用 super() function 但结果是一样的。 我 ...

Python 的 Super() 如何在多个 inheritance 中为超类工作? - How Python's Super() works in multiple inheritance for superclasses?

我试图了解 super 如何在 python 的多个 inheritance 中为超类工作,例如在下面的代码中: 如果我删除super().__init__(**kwargs)执行结束 '第三' object 没有属性 'parm2' 仅打印parm1和parm3 ,即使我在Class Third ...


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