multiple-inheritance

How can I require a method argument in Java to implement multiple interfaces?

被刻印的时光 ゝ 提交于 2019-11-28 05:39:24
It's legal to do this in Java: void spew(Appendable x) { x.append("Bleah!\n"); } How can I do this (syntax not legal): void spew(Appendable & Closeable x) { x.append("Bleah!\n"); if (timeToClose()) x.close(); } I would like if possible to force callers to use objects that are both Appendable and Closeable, without requiring a specific type. There are multiple standard classes that do this, e.g. BufferedWriter, PrintStream, etc. If I define my own interface interface AppendableAndCloseable extends Appendable, Closeable {} that won't work since the standard classes that implement Appendable and

How to create a class that “extends” two existing concrete classes

狂风中的少年 提交于 2019-11-28 05:26:00
问题 I very well know that it can be done with the help of interfaces and i have done it many times. But this time my situation is quite difference. I have class A , class B and i need to create another class C which extends both A and B because C should have boths functionality and also note that A and B are not inter related so even i cant say A may extend class B . I am quite confused what should i do right now. I know we cant change java... but at least there would be some way possible. Even

pyqt5 and multiple inheritance

为君一笑 提交于 2019-11-28 05:21:18
问题 I'd like to create a new class that inherits two subclasses of QWidget. I know multi-inheritance isn't possible in pyqt, but how could I manage to have the properties of both parent classes in one subclass? What I wish I could do is as follows: class A(QWidget): def __init__(self, widget, parent=None): widget.destroyed.connect(self.destroy_handler) @pyqtSlot() def destroy_handler(self): pass class B (A, QStatusBar): def __init__(self, widget, parent=None): A.__init__(self, widget) QStatusBar.

Python and order of methods in multiple inheritance

守給你的承諾、 提交于 2019-11-28 04:07:04
问题 In Python, if you define two classes with the same method and intend for those two classes to be parent classes, as: class A(object): def hello(self): print "hello from class a" and: class B(object): def hello(self): print "hello from class b" when you define the child class and add the two parent classes in the order A and B: class C(A, B): def __init__(self): self.hello() the method that is used when calling self.method() is the one belonging to A, or the first class in the list of

Java 8 default method inheritance

孤者浪人 提交于 2019-11-28 02:56:06
问题 Let's say there are following types: public interface Base { default void sayHi(){ System.out.println("hi from base"); } } public interface Foo extends Base { @Override default void sayHi(){ System.out.println("hi from foo"); } } public interface Bar extends Base { } public class MyClass implements Foo, Bar { public static void main(String[] args) { MyClass c = new MyClass(); c.sayHi(); } } In this scenario, if main is executed, "hi from foo" is printed. Why does Foo 's implementation take

How to solve “Must be MarshalByRefObject” in a good but multiple-inheritance amputated language like C#?

霸气de小男生 提交于 2019-11-28 02:38:19
问题 How to solve "Must be MarshalByRefObject" in a good but multiple-inheritance amputated language like C#? The problem is very simple, in several cases you just have to inherit from this class (infrastructure requirements). It does not matter here really, which cases. So, what do you do if you've already inherited from some other class (your domain model requirements)? Btw good application frameworks, like spring.net always make sure you DON'T have to inherit from this class no matter what kind

Multiple inheritance with one base class

▼魔方 西西 提交于 2019-11-28 02:26:30
(Removed original text as it is unrelated to the current question which has already been answered. See revisions.) Here is my example test.hpp (simplified): class House { private: int nWindows; public: House(int nWindows); int getNumberOfWindows(); }; class PaintedHouse : public virtual House { private: int colorCode; public: PaintedHouse(int nWindows, int colorCode); int getColorCode(); }; class OccupiedHouse : public virtual House { private: int nPeople; public: OccupiedHouse(int nWindows, int nPeople); int getNumberOfPeople(); }; class PaintedOccupiedHouse : public PaintedHouse,

Virtual multiple inheritance - final overrider

时光总嘲笑我的痴心妄想 提交于 2019-11-28 01:58:33
问题 while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon the following example: #include<iostream> using namespace std; class Base { public: virtual void f(){ cout << "Base.f" << endl; } }; class Left : public virtual Base { }; class Right : public virtual Base{ public: virtual void f(){ cout << "Right.f" << endl; } }; class Bottom : public Left, public Right{ }; int main(int argc,char **argv) { Bottom* b = new Bottom(); b->f(); } The above, somehow, compiles and

Multiple Inheritance : size of class for virtual pointers?

前提是你 提交于 2019-11-28 01:15:39
Given the code: class A{}; class B : public virtual A{}; class C : public virtual A{}; class D : public B,public C{}; int main(){ cout<<"sizeof(D)"<<sizeof(D); return 0; } Output: sizeof(D) 8 Every class contains its own virtual pointer only not of any of its base class, So, why the Size of class(D) is 8? It depends on compiler implementation. My compiler is Visual Stdio C++ 2005. Code like this: int main(){ cout<<"sizeof(B):"<<sizeof(B) << endl; cout<<"sizeof(C):"<<sizeof(C) << endl; cout<<"sizeof(D):"<<sizeof(D) << endl; return 0; } It will output sizeof(B):4 sizeof(C):4 sizeof(D):8 class B

Why is this an ambiguous MRO?

亡梦爱人 提交于 2019-11-28 01:09:20
class First(object): def __init__(self): print "first" class Second(First): def __init__(self): print "second" class Third(First, Second): def __init__(self): print "third" Source Why can't Python create a consistent MRO? It seems to me it's pretty clear: Search in First if method does not exist in Third Search in Second if method does not exist in First But if you try it out: TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases First, Second To be "consistent" the MRO should satisfy these constraints: If a class inherits from