标签: multiple-inheritance

如何引用具有多个边界的泛型返回类型

我最近看到可以声明一个也受接口限制的返回类型.考虑以下类和接口:

public class Foo {
    public String getFoo() { ... }
}

public interface Bar {
    public void setBar(String bar);
}
Run Code Online (Sandbox Code Playgroud)

我可以声明一个这样的返回类型:

public class FooBar {
    public static <T extends Foo & Bar> T getFooBar() {
        //some implementation that returns a Foo object,
        //which is forced to implement Bar
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我调用该方法从什么地方,我的IDE告诉我,在返回类型的方法String getFoo(),以及setBar(String),但只有当我点了后面的点功能如下:

FooBar.getFooBar(). // here the IDE is showing the available methods.
Run Code Online (Sandbox Code Playgroud)

有没有办法获得对这样一个对象的引用?我的意思是,如果我做这样的事情:

//bar only has the method setBar(String)
Bar bar = FooBar.getFooBar(); …
Run Code Online (Sandbox Code Playgroud)

java generics interface multiple-inheritance

38
推荐指数
2
解决办法
1万
查看次数

C ++钻石问题-如何仅一次调用基本方法

我在C ++中使用多重继承,并通过显式调用基本方法来扩展基本方法。假定以下层次结构:

     Creature
    /        \
 Swimmer    Flier
    \        /
       Duck
Run Code Online (Sandbox Code Playgroud)

对应于

     Creature
    /        \
 Swimmer    Flier
    \        /
       Duck
Run Code Online (Sandbox Code Playgroud)

现在,这带来了一个问题-调用duck的print方法将调用其各自的基本方法,所有这些方法都依次调用该Creature::print()方法,因此最终被两次调用-

I'm a creature
I can fly
I'm a creature
I can swim
I'm a duck
Run Code Online (Sandbox Code Playgroud)

我想找到一种方法来确保基本方法仅被调用一次。与虚拟继承的工作方式类似(在第一次调用时调用基本构造函数,然后仅在来自其他派生类的后续调用中为其分配一个指针)。

是否有一些内置方法可以做到这一点,还是我们需要依靠自己实现?

如果是这样,您将如何处理?

这个问题并非特定于打印。我想知道是否有一种机制可以扩展基本方法和功能,同时保持调用顺序并避免出现钻石问题。

我现在知道,最突出的解决方案是添加辅助方法,但是我只是想知道是否存在“更清洁”的方法。

c++ multiple-inheritance diamond-problem

37
推荐指数
6
解决办法
3874
查看次数

.NET中的多重继承有哪些好的替代方案?

在WPF应用程序中,我的类层次结构遇到了一些问题.这是将两个继承树合并在一起的问题之一,如果没有多重继承,就无法找到任何合理的方法来使继承顺利运行.我想知道是否有人有任何明智的想法让这种系统工作,而不会让它无法遵循或调试.

我是一名低级工程师,所以我的第一个想法总是,"哦!我只是用原生C++写一些这些类,并在外部引用它们!然后我可以让我所有的老派OO好玩!" 唉,当你需要从托管控件继承时,这没有用...

请允许我显示当前投影类图的片段:

 ____________________________________      _____________________________________
| CustomizableObject                 |    | System.Windows.Controls.UserControl |
|____________________________________|    |_____________________________________|
|   string XAMLHeader()              |                        ?
|   string XAMLFooter()              |?--?                    |
|   CustomizableObject LoadObject()  |   \                    |
|   <Possible other implementations> |    \                   |
|____________________________________|     \                  |
         ?                      ?           \                 |
         |                      |            \                |
         |                      |             \               |
 _________________    ______________________   \    _____________________
| SpriteAnimation |  | SpriteAnimationFrame |  ?---| CustomizableControl |
|_________________|  |______________________|      |_____________________|
                                                      ?             ?
                                                      |             |
                                                      |             |
                                                  ________    _____________
                                                 | Sprite …
Run Code Online (Sandbox Code Playgroud)

.net c# inheritance multiple-inheritance .net-3.5

36
推荐指数
3
解决办法
7355
查看次数

解决元类冲突

我需要根据某些条件创建一个使用不同基类的类.有些课程让我臭名昭着:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
Run Code Online (Sandbox Code Playgroud)

一个例子是sqlite3,这是一个简单的例子,你甚至可以在解释器中使用:

>>> import sqlite3
>>> x = type('x', (sqlite3,), {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
Run Code Online (Sandbox Code Playgroud)

python metaclass multiple-inheritance python-3.x

36
推荐指数
6
解决办法
4万
查看次数

使用实现多个接口pre-generics的参数

假设我有这些接口:

public interface I1 {
  void foo();
}

public interface I2 {
  void bar();
}
Run Code Online (Sandbox Code Playgroud)

和班级:

public class A extends AParent implements I1, I2 {
   // code for foo and bar methods here
}

public class B extends BParent implements I1, I2 {
  // code for foo and bar methods here
}

public class C extends CParent implements I1 {
  // code for foo method here
}
Run Code Online (Sandbox Code Playgroud)

现在,使用泛型我可以有一个方法,如:

public <T extends I1 & I2> void method(T param) {
  param.foo();
  param.bar(); …
Run Code Online (Sandbox Code Playgroud)

java generics parameters multiple-inheritance

35
推荐指数
2
解决办法
3万
查看次数

普通的Class可以实现多个接口吗?

我知道接口之间可以有多种继承,例如:

public interface C extends A,B {...} //Where A, B and C are Interfaces
Run Code Online (Sandbox Code Playgroud)

但是有可能从多个接口继承常规类,如下所示:

public class A implements C,D {...} //Where A is a Class and C and D are interfaces
Run Code Online (Sandbox Code Playgroud)

java multiple-inheritance

33
推荐指数
4
解决办法
9万
查看次数

在可变参数模板中使用声明

这个问题的灵感来自以下多重继承重载伪歧义的解决方案,这是一个很好的方法来实现此答案中提出的boost :: variant的lambda访问者:

我想做类似以下的事情:

template <typename ReturnType, typename... Lambdas>
struct lambda_visitor : public boost::static_visitor<ReturnType>, public Lambdas... {
    using Lambdas...::operator(); //<--- doesn't seem to work
    lambda_visitor(Lambdas... lambdas) : boost::static_visitor<ReturnType>() , Lambdas(lambdas)... { }
};
Run Code Online (Sandbox Code Playgroud)

我不确定在打包类型列表中添加使用子句的正确语法是什么.该using条款对于阻止编译器抱怨operator()模糊不清是完全不重要的,因为它们具有所有不同的签名.

c++ operator-overloading multiple-inheritance variadic-templates c++11

30
推荐指数
2
解决办法
3729
查看次数

试图继承三个基类而不能

我有几个与User课堂设计有关的问题,但它们不同,我认为它们应该是独立的问题.

所以,第一个与基类的继承有关.我目前继承两个类,ProfileBaseISessionMgrEntry为这样的:

public class User : ProfileBase, ISessionMgrEntry
Run Code Online (Sandbox Code Playgroud)

但是,我还想继承第三个类MembershipUser,如下所示:

public class User : ProfileBase, MembershipUser, ISessionMgrEntry
Run Code Online (Sandbox Code Playgroud)

但是,编译器不会让我这样做.为什么?而且,我该如何解决这个问题呢?

谢谢.

PS - ASP.NET 3.5/C#

编辑

你好.我认为以下解决方案可能适用于我想要实现的目标.看起来非常简单直接.我这样做,所以我可以创建一个完整/组合的User对象.有没有人看到任何可能导致问题的原因?我在叮叮当当时出现的是重叠属性.例如,两者MembershipUserProfileBase分享" UserName".我应该选择其中一个还是这个设计缺陷?建议?再次感谢.

 public class User
    {

        #region Constructors
            private readonly MembershipUser _MembershipUser;
            private readonly ProfileBase _ProfileBase;
        #endregion

        public User()
        {
            _MembershipUser = new MembershipUser();
            _ProfileBase = new ProfileBase();

        }

        public string Comment
        {
            get { return _MembershipUser.Comment as string; }
            set { …
Run Code Online (Sandbox Code Playgroud)

c# inheritance multiple-inheritance

28
推荐指数
3
解决办法
7万
查看次数

如何避免使用super()进行无限递归?

我有这样的代码:

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

class B(A):
    def __init__(self):
        self.b = 2
        super(self.__class__, self).__init__()

class C(B):
    def __init__(self):
        self.c = 3
        super(self.__class__, self).__init__()
Run Code Online (Sandbox Code Playgroud)

实例化B按预期工作但实例化C无限递归并导致堆栈溢出.我怎么解决这个问题?

python oop multiple-inheritance super

28
推荐指数
1
解决办法
4815
查看次数

Java接口上的多重继承

我认为多重继承在Java中总是非法的,但是这段代码编译:

public interface A {
  void a();
}

public interface B {
  void b();
}

public interface AB extends A, B {
}
Run Code Online (Sandbox Code Playgroud)

有一个空的界面,如AB被认为是一个坏的做法?有没有办法在避免空接口(使用泛型或其他方式)的同时实现类似的东西?

注意:我不是在问如何通过接口模拟多重继承.我意识到我可以做到以下几点:

public class AbImpl implements A, B {
  public void a() {}
  public void b() {}
}
Run Code Online (Sandbox Code Playgroud)

由于各种原因,我需要一个具有两种方法的接口.

java multiple-inheritance

28
推荐指数
3
解决办法
4万
查看次数