根据这个答案,如何__new__和__init__应该在Python中工作,
我编写了这段代码来动态定义和创建一个新的类和对象.
class A(object):
def __new__(cls):
class C(cls, B):
pass
self = C()
return self
def foo(self):
print 'foo'
class B(object):
def bar(self):
print 'bar'
a = A()
a.foo()
a.bar()
Run Code Online (Sandbox Code Playgroud)
基本上,因为__new__A返回一个继承A和B的动态创建的C,它应该有一个属性bar.
为什么C 不能有一个bar属性?
继承两个基类时,如果两者都有一个具有相同名称和签名的方法会发生什么?
class Physics
{
public:
void Update() { std::cout << "Physics!" }
};
class Graphics
{
public:
void Update() { std::cout << "Graphics!" }
};
class Shape : Physics, Graphics
{
};
int main()
{
Shape shape;
shape.Update();
}
Run Code Online (Sandbox Code Playgroud)
会发生什么?
假设我们有一个具体的类A和一个抽象类B.
考虑一个具体的C,它继承自A和B,并实现B:
class C : public A, public B
{
/* implementation of B and specific stuff that belongs to C */
};
Run Code Online (Sandbox Code Playgroud)
现在我定义一个签名是什么的函数 void foo(B* b);
这是我的代码,我可以假设每个指向B的指针都是A和B.在foo的定义中,如何获得指向A的指针?一个讨厌但有效的技巧是将指针对齐,如下所示:
void foo(B* b)
{
A* a = reinterpret_cast<A*>(reinterpret_cast<char*>(b) - sizeof(A));
// now I can use all the stuff from A
}
Run Code Online (Sandbox Code Playgroud)
请记住,C没有超类型,实际上,有许多类似于C的类,只有A和B.可以自由地质疑我的逻辑和这个设计样本,但问题只是关于指针对齐.
我希望能够打电话给'person.Person.类 "并取回'类person.Person’,像它用于一个Person对象:
>>> p = person.Person()
>>> p.__class__
<class 'person.Person'>
>>> person.Person.__class__
<class 'perframe.datatype.TypeSystemMeta'>
Run Code Online (Sandbox Code Playgroud)
这是Person继承树......
class TypeSystem(object):
__metaclass__ = TypeSystemMeta
class Model(TypeSystem):
pass
class Node(Vertex,Model):
pass
class Person(Node):
pass
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
python reflection inheritance multiple-inheritance method-resolution-order
这可能听起来很愚蠢,但是如果每个类都隐式扩展了Object类并且允许再扩展一个类,那么它怎么不是多重继承呢?从用户的角度来看,可能有人认为它们不支持多重继承,因为不允许用户扩展多个类.但是,这些语言似乎具有对多重继承的内部支持,而这种继承只是为了保持简单而不向用户公开.我有道理吗?
注意:我不支持或反对支持多重继承.只是想澄清一些想法.
虽然我试图访问两个接口中给出的相同方法,但我理解多重继承的概念.例:-
interface Interface1{
int show();
void display();
}
Run Code Online (Sandbox Code Playgroud)
interface Interface2 {
int show();
void display();
}
Run Code Online (Sandbox Code Playgroud)
class Impl implements Interface1, Interface2 {
// how to override show() and display() methods such that
// i could access both the interfaces
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
class A(object):
def __init__(self):
print("A.__init__")
super(A, self).__init__() # 1
print("A.__init__ finished")
class B(A):
def __init__(self):
print("B.__init__")
super(B, self).__init__() # 2
print("B.__init__ finished")
class C(A):
def __init__(self):
print("C.__init__")
super(C, self).__init__()
print("C.__init__ finished")
class D(B, C):
def __init__(self):
print("D.__init__")
print("Initializing B")
B.__init__(self) # 3
print("B initialized")
print("Initializing C")
C.__init__(self) # 4
print("C initialized")
print("D.__init__ finished")
D()
# D.__init__
# Initializing B
# B.__init__
# C.__init__
# A.__init__
# A.__init__ finished
# C.__init__ finished
# B.__init__ finished
# B initialized
# Initializing …Run Code Online (Sandbox Code Playgroud) python initialization multiple-inheritance super diamond-problem
我正在学习Java,我正在做这个练习,我不得不说主要方法打印的是什么.其目标是更好地理解Java继承.
interface X extends Remote{
A m(B bb) throws RemoteException;
}
class A implements Serializable{
int a;
A m(A aa){
aa.a = 3;
System.out.println("A.m");
return aa;
}
}
class B extends A{
int b;
A m(B bb){
bb.b = 7;
System.out.println("B.m");
return bb;
}
}
class C extends A implements X{
public B m(B bb){
if(bb.b == 7) bb.b = 9; else bb.b = 1;
System.out.println("C.m");
return bb;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个主要的方法,我打电话:
X x = new C();
B b = new …Run Code Online (Sandbox Code Playgroud) 假设我有一个酒类,它有两个派生类:葡萄酒和啤酒.如果我将制作一个继承了Wine和Beer的类Cider,会发生什么?
在Cider类中会出现多少份酒精?
我知道它可以通过虚拟继承来完成而没有它,但会有什么区别?
我有头等舱.
class Class1
{
public:
Class1();
double getVal();
bool operator<(Class1& A) { return getVal() > A.getVal(); }
virtual ~Class1();
}
Run Code Online (Sandbox Code Playgroud)
从这个课程我得到两个班级.
class Class2 : virtual public Class1
{
protected:
int val1;
public:
int getVal(){ return val; }
bool operator<(Class1& A) { return getVal() > A.getVal(); }
}
Run Code Online (Sandbox Code Playgroud)
而第二节课.
class Class3 : virtual public Class1
{
protected:
double val2;
public:
double getVal(){ return val; }
bool operator<(Class1& A) { return getVal() > A.getVal(); }
}
Run Code Online (Sandbox Code Playgroud)
最后从这些课程中我得到了一个课程.
class Class4 : public Class2, …Run Code Online (Sandbox Code Playgroud)