Python 调用父类函数比较父类和子类

Python 调用父类函数比较父类和子类,python,c++,class,methods,Python,C++,Class,Methods,我正在学习Python课程,突然想到了使用parents重写函数来比较父类和子类。基本上: class A(object): def __init__(self,a): self.a = a def __lt__(self,other): return self.a < other.a class B(A): def __init__(self,a,b): self.a = a self.b = b

我正在学习Python课程,突然想到了使用parents重写函数来比较父类和子类。基本上:

class A(object):
    def __init__(self,a):
        self.a = a

    def __lt__(self,other):
        return self.a < other.a

class B(A):
    def __init__(self,a,b):
        self.a = a
        self.b = b

    def __lt__(self,other):
        return self.b < other.b

a = A(2)
b = B(1,3)
print(a < b)
#print(b < a) # AttributeError: 'A' object has no attribuite 'b'
print(A.__lt__(b,a)) # so we call this instead
A类(对象):
定义初始化(self,a):
self.a=a
定义(自身、其他):
返回自我a
现在,我想用C做同样的事情++

class A{
    int a;
public:
    A(int a) : a(a) {}
    bool operator<(A t){ return a < t.a; }
};

class B: public A{
    int b;
public:
    B(int a, int b) : A(a), b(b) {}
    bool operator<(B t){ return b < t.b; }
};

int main()
{
    A a(2);
    B b(3,1);

    std::cout << (a < b) << std::endl;
    //std::cout << (b < a); // error, A has no b attribute
    //std::cout << A::operator<(dynamic_cast<A&>(b),a); // basically what I would like to happen
    std::cout << a.operator<(dynamic_cast<A&>(b)) << std::endl; // here I would like to reverse a and b

    return 0;
}
A类{
INTA;
公众:
A(inta):A(A){}

BOOL运算符< P>免责声明:这样的事情最好不要在实际代码中完成,至少要进行比较。这只是一些C++构造的一个示例使用。 变量1:静态调度

class A{
    int a;
public:
    A(int a) : a(a) {}
    friend bool operator<(A& x, A& y){ return x.a < y.a; }
};

class B: public A{
    int b;
public:
    B(int a, int b) : A(a), b(b) {}
    friend bool operator<(B& x, B& y){ return x.b < y.b; }
};
a
在比较中的行为类似于
a
。系统基于运算符重载


变量2:动态调度

class A;
class B;

class A{
    int a;
public:
    A(int a) : a(a) {}
    virtual ~A() {}
    bool operator<(A& t){ return t.compare(*this); }
protected:
    virtual bool compare (A& t);
    virtual bool compare (B& t);
};

class B: public A{
    int b;
public:
    B(int a, int b) : A(a), b(b) {}
protected:
    bool compare (A& t) override;
    bool compare (B& t) override;
};

bool A::compare(A& t) { return t.a < a; }
bool A::compare(B& t) { return t.a < a; }
bool B::compare(A& t) { return A::compare(t); }
bool B::compare(B& t) { return t.b < b; }

a
在比较中的行为类似于
B
。该系统基于双重动态调度,有时也称为访客模式。

如果您比较两个真正是
B
对象的
a&
对象,您希望发生什么?即
bx;a&y=x;返回y
?是否应该比较
一个<代码> >或代码> b>代码>成员,如果我在<代码> >代码> >代码>,就应该用<代码>操作符比较它们,这样做的二进制操作大部分是没有意义和危险的。你可以实现你想玩弄的东西,并探索C++是如何工作的,但是如果你用真实的代码来做,你会后悔的。t.请给我们一个真实世界的例子,说明您正在尝试做什么。也许需要更好的设计选择。这是一个纯粹的理论问题,因为同样的事情在Python中也很容易实现。谢谢,这回答了这个问题,并给了我一些要学习的东西。
class A;
class B;

class A{
    int a;
public:
    A(int a) : a(a) {}
    virtual ~A() {}
    bool operator<(A& t){ return t.compare(*this); }
protected:
    virtual bool compare (A& t);
    virtual bool compare (B& t);
};

class B: public A{
    int b;
public:
    B(int a, int b) : A(a), b(b) {}
protected:
    bool compare (A& t) override;
    bool compare (B& t) override;
};

bool A::compare(A& t) { return t.a < a; }
bool A::compare(B& t) { return t.a < a; }
bool B::compare(A& t) { return A::compare(t); }
bool B::compare(B& t) { return t.b < b; }
B b(0, 42);
A& a = b;