在Python 网页上的文档中,python中经典类的方法解析顺序被描述为深度优先从左到右搜索。我试图用这段代码来测试这个:
class A(object):
def __init__(self):
print "Initialized A"
class B(A):
def test():
print "Initialized B"
class C(A):
def __init__(self):
print "Initialized C"
class D(B, C):
def __init__(self):
super(D, self).__init__()
print "Initialized D"
Run Code Online (Sandbox Code Playgroud)
当我创建对象 D 的实例时:
D()
Run Code Online (Sandbox Code Playgroud)
我得到结果:
Initialized C
Initialized D
Run Code Online (Sandbox Code Playgroud)
虽然我期望打印“初始化 A”、“初始化 D”,因为 MRO 是深度优先的,但首先在 B 中搜索初始化,如果找不到(就是这种情况),它应该在层次结构中更深入并查找B(即A)的基类中的函数。有人能解释一下为什么我得到 C 的 init 函数而不是 A 的那个吗?
#include<iostream>
using namespace std;
class base
{
public:
virtual void f(){}
};
class middle1:public base
{};
class middle2:public base
{};
class derive:public middle1,public middle2
{};
int main()
{
derive* pd=new derive();
pd->f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道 virtual 解决了这个问题,但如何解决?即使我们没有多重继承,我们是否可以始终编写公共虚拟以确保安全。
我有一个从 2 个抽象模型继承的模型,它们的元类中有属性。
我怎样才能让它从两个父类继承元数据?似乎它只继承了首先编写的类的元数据。
说我有两个基类,
struct A {};
struct B {};
Run Code Online (Sandbox Code Playgroud)
以及使用多重继承的派生类
struct D : A, B {};
Run Code Online (Sandbox Code Playgroud)
如果我的使用场景是这样的:
A *obj = new D;
Run Code Online (Sandbox Code Playgroud)
即我永远不会使用B基类来引用派生对象,我是否必须使两个基础的析构函数虚拟?我目前正在申报的析构函数B为protected禁止从这样其他用户,但是否足够?
析构函数怎么样D?
c++ destructor protected multiple-inheritance virtual-destructor
以下代码:
class City {
var cityId : String?
var cityName : String?
}
class Town {
var townid : String?
var townName : String?
}
class Address : City , Town {
var house : String?
var street: String?
}
Run Code Online (Sandbox Code Playgroud)
生成编译时错误:
Address.swift:38:24: 来自类“City”和“Town”的多重继承
我怎样才能解决他的那种问题?遵循什么方法?
我对某些 Python 类中的多重继承的设计有一些疑问。
问题是我想扩展 ttk 按钮。这是我最初的建议(我省略了缩短方法中的所有源代码,除了init方法):
import tkinter as tk
import tkinter.ttk as ttk
class ImgButton(ttk.Button):
"""
This has all the behaviour for a button which has an image
"""
def __init__(self, master=None, **kw):
super().__init__(master, **kw)
self._img = kw.get('image')
def change_color(self, __=None):
"""
Changes the color of this widget randomly
:param __: the event, which is no needed
"""
pass
def get_style_name(self):
"""
Returns the specific style name applied for this widget
:return: the style name as a …Run Code Online (Sandbox Code Playgroud) tkinter multiple-inheritance ttk method-resolution-order python-3.x
在下面的这个例子中,m类上的方法A只被调用一次。
我知道这是一个功能,这是解决在这种类似菱形的继承场景中A'sm方法将被调用两次(如果它以幼稚的方式实现)的问题的 Pythonic 方法。
这一切都在这里描述:https :
//www.python-course.eu/python3_multiple_inheritance.php
(1) 但是在幕后……他们是如何实现这种行为的,即该类A的m方法只被调用一次?!
简单地询问:在执行过程中哪一行被“跳过” - 是 line#1还是 line # 2?
有人可以对此有更多的了解吗?
我从来没有认真使用过多重继承,因为我主要用 Java 编程。所以我真的很好奇这里的这个场景,更具体地说是它背后的内部运作。
注意:我只是想大致了解一下它在 Python 中的工作原理,而不是真正了解这里的每一个细节。
(2) 如果我想(在同样的场景中并且出于某种原因)A的m方法被调用两次(或N次数取决于D我们有多少基类),同时仍然使用super(). 这可能吗?是否super()支持这种操作方式?
(3) 这只是一些树或DAG访问算法,它们会跟踪哪个类的m方法已经被访问过并且只是不访问(调用它)两次?如果是这样,那么简单地说,我猜“#2”是被跳过的行。
class A:
def m(self):
print("m of A called")
class B(A):
def m(self):
print("m of B called") …Run Code Online (Sandbox Code Playgroud) 我有一个 Blazor 组件,它只有一个 razor 文件(后面没有 razor.cs 代码)并希望从接口继承该组件,首先我尝试了以下操作:
MyComponent.razor :
@inherits IMyInterface
Run Code Online (Sandbox Code Playgroud)
然后在构建后,我在 .cs 生成的代码(由 Visual Studio 中)中得到一个错误,我意识到该组件仅从我的界面而不是ComponentBase类派生。这是生成的代码:
...
public partial class MyComponent: IMyInterface
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
}
#pragma warning restore 1998
}
...
Run Code Online (Sandbox Code Playgroud)
所以我得到了“找不到合适的方法来覆盖”错误。
然后我尝试了以下代码并得到了所有这些代码的编译错误:
// 1) multiple inheritance
@inherits ComponentBase,IMyInterface
// 2) adding semicolon
@inherits ComponentBase,IMyInterface;
// 3) mutiple @inherits directive
@inherits ComponentBase
@inherits IMyInterface
Run Code Online (Sandbox Code Playgroud)
通过在文件MyComponent.razor.cs后面添加代码并在那里编写继承代码解决了问题,但我想知道是否可以在 razor 文件中具有多个接口继承?
#include <iostream>
using namespace std;
class A {
int a;
};
class B1 : virtual public A {
int b1;
};
class B2 : virtual public A {
int b2;
};
class C : public B1, public B2 {
int c;
};
int main() {
A obj1; B1 obj2; B2 obj3; C obj4;
cout << sizeof(obj1) << endl;
cout << sizeof(obj2) << endl;
cout << sizeof(obj3) << endl;
cout << sizeof(obj4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
4
16
16 …Run Code Online (Sandbox Code Playgroud) 更新:所以这里的每个人都告诉我,我只需要重新开始我如何设计课程(顺便说一句,谢谢大家的优秀答案!).接下来,我开始对战略模式进行广泛阅读.我想创建从抽象基类继承的行为类(或策略类).然后,Candidate类将具有不同抽象基类/类的属性作为Type行为或策略.也许是这样的:
public abstract class SalaryStrategy {
public abstract decimal Salary { get; set; }
public abstract decimal Min { get; set; }
public abstract decimal Mid { get; set; }
public decimal CompaRatio {
get {
if (this.Mid == 0) { return 0; }
else { return this.Salary / this.Mid; }
}
}
}
public class InternalCurrentSalaryStrategy {
public override decimal Salary { get; set; }
public override decimal Min {
get { return this.Salary * …Run Code Online (Sandbox Code Playgroud) c# inheritance code-reuse design-patterns multiple-inheritance
c++ ×3
python ×3
c# ×2
inheritance ×2
python-3.x ×2
asp.net-core ×1
blazor ×1
code-reuse ×1
destructor ×1
django ×1
metaclass ×1
oop ×1
protected ×1
razor ×1
swift ×1
tkinter ×1
ttk ×1
virtual ×1