- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个类集合,A1、A2、A3 等,它们都有方法 m()
。我还有 B 类方法 m()
。我希望能够轻松地创建类 C1、C2、C3 等,它们从类 B 调用 m()
,同时还具有 A1、A2、A3 等的所有其他属性......
但是,我遇到的问题是,在 C1 类中,B 类的方法 m()
应该调用 A1 类的 m()
。
我很难用语言表达我想要的东西,但我目前正在考虑这样做的方式是使用 mixins。 C1 将从 A1 继承,混合 B。但是,我不知道如何使 B 中的 m()
调用正确的 m()
A类之一。
那么,我的两个问题:
编辑:根据要求,一个具体的例子:A1、A2、A3 等中的方法m(p)
都计算一个矩阵M
,对于一些参数p
。我想创建类 C1、C2、C3 等,它们的行为方式与 A1、A2、A3 相同,except 方法 m()
。新方法 m()
采用更长的参数列表 p
,大小为 N,我们计算 A*.m()
N 次然后返回总和。
计算 m()
总和的代码对于所有 A* 类都是相同的。在上面建议的混合解决方案中,求和代码将在 B 中。B 和 A1 将被继承以形成 C1。但是,B 的 C1
中的方法 m()
必须调用 A1.m()
。
最佳答案
我认为您只需要 super
即可将调用重定向到父类或兄弟类(取决于 MRO)。
例如:
class A1(object):
def m(self):
print('Calling method m of class A1')
self.data *= 2
class A2(object):
def m(self):
print('Calling method m of class A2')
self.data *= 3
class A3(object):
def m(self):
print('Calling method m of class A3')
self.data *= 4
class B(object):
def m(self, p):
print('Calling method m of class B')
for i in range(p):
# You haven't specified which python you are using so I assume
# you might need to most explicit variant of super().
# Python3 also allows just using super().m()
super(B, self).m()
class C1(B, A1):
def __init__(self, value):
self.data = value
只是测试一下:
a = C1(10)
a.m(10)
打印:
Calling method m of class B
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
和保存的值:
a.data
# returns 10485760
定义其他 C
也可以:
class C2(B, A2):
def __init__(self, value):
self.data = value
a = C2(10).m(2)
#Calling method m of class B
#Calling method m of class A2
#Calling method m of class A2
class C3(B, A3):
def __init__(self, value):
self.data = value
a = C3(10).m(1)
#Calling method m of class B
#Calling method m of class A3
当然,您需要另一个逻辑并且可能需要从 .m()
返回值而不是就地修改,但我认为您可以自己解决。
您要查找的词可能是 MRO (method resolution order) .希望对您有所帮助。
同样感兴趣的可能是 super
(Python2) 的文档。 , super
(Python3) .
并且您始终可以通过调用 .mro()
方法来检查类的 MRO
:
print(C1.mro())
[<class '__main__.C1'>, <class '__main__.B'>, <class '__main__.A1'>, <class 'object'>]
因此 python 首先检查 C1
是否有方法 m
,如果没有则检查 B
。 B
有一个所以它被执行。 super
调用然后再次进入 MRO
并检查下一个类 (A1
) 是否有方法 m
,然后执行。
关于python - Mixin 覆盖继承的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35561346/
乐 mixins具有两个(或更多)性质,在同一个容器中组合多个值,或者值与角色一起。但是,据我所知,没有一种直接的方法可以检查不是由您创建的变量中的“混合性”。 这可能是个伎俩 my $foo = 3
我对 sass 比较陌生,但是当我学习它时,Sass 网站说要开始使用 @use 而不是 import,所以经过大量的反复试验,我终于弄清楚了如何使用它与导入相同。 注意:我使用 Prepros 进行
给定一个看起来像这样的代码片段,我如何编写一个函数来检查给定对象是否实现了某个混合?我尝试使用指针转换,但由于它们具有相同的基础,因此每个结果都是非空的,但我猜测有一个模板化的解决方案,但找不到我可以
我正在使用 Typescript 2.2 的 mixin,并且想使用另一个 mixin 的属性。 文档显示可以将 mixins 限制为仅混合到某些类中...... const WithLocation
我如何创建一个将嵌套的 mixin 属性用作参数的 mixin? 我用下一个例子来解释自己。 我有一个“过渡属性”mixin: .transition-property (@props){ -we
我浏览了language documentation而且 Google Dart 似乎不支持 mixins(接口(interface)中没有方法主体,没有多重继承,没有类似 Ruby 的模块)。我对此
我想编写返回混合的函数/混合。例如我有这个 mixin: @mixin generate-offsets-from-map($class-slug,$type,$from, $to, $step) {
所有 Less 文档和教程都使用 #namespace > .mixin()当它进入命名空间时的语法。但是我发现自己更习惯于 .namespace.mixin()语法,即: .namespace()
我正在努力实现以下目标: class A { def foo() { "foo" } } class B { def bar() { "bar" } } A.mixin B def a = n
出于本问题的目的,将“mixin”视为 https://www.typescriptlang.org/docs/handbook/mixins.html 中所述的函数。 .在这种情况下,mixin 扩
如何在 vue mixins 中组合两个函数? Vue.mixin({ methods: { functionOne: () => { console.log(1)
我需要重写 mixin 添加的一些成员函数来自第三方库。问题是:mixin 立即在多个第 3 方类定义中使用,在定义 mixin 的同一个脚本文件中。我只能在此脚本之前或之后插入自定义代码,而不能在两
我有一些基本的 mixin,它们使用媒体查询应用一些规则 .on-small(@rules) { @media (@minWidthSmall) { @rules(); } } .on-mediu
我尝试安装 npm 包。所有软件包都安装正确。 但是当我尝试使用 npm start 运行应用程序时当时发生以下错误: ERROR in ./node_modules/css-loader?{"sou
这里有两个mixin @mixin parent { .parent & { @content; } } @mixin child($child) { .#{$child} & {
我在另一个 mixins 中有一个 mixins .background(@url: @base-url , @repeat: repeat, @pos1: left, @pos2: center
我有这个: 如您所见,我目前有一个包含按钮样式混合宏的条件,无论如何我可以自动包含一个吗?例如: @mixin button($color) @include button-#{$color} 最
我有以下代码,可以很好地将各种 std::tuples 转发到我的“BaseSensor”主机类的各种 mixin。 #include // std::cout std::endl #include
我按照概述的方式组织我的 sass (scss) 文件 here ... stylesheets/ | |-- modules/ # Common modules | |
所以,这是我的第一个 mixin .3transitions (@value1,@value2,@value3,@duration){ @value: ~"@{value1},@{value2}
我是一名优秀的程序员,十分优秀!