没有多重inheritance且没有代码重复的多重inheritance

我有一个关于如何在不允许多重inheritance的语言中处理以下场景的理论问题。

想象一下,我有一个基类Foo ,从中我希望创建三个子类:

  • Class BarinheritanceFoo并实现function“A”
  • Baz类inheritanceFoo并实现function“B”
  • QuxinheritanceFoo并实现function“A”和“B”

想象一下,实现function“A”和“B”的代码总是相同的。 有没有办法只为“A”和“B”编写一次代码,然后将相应的类应用(或“inheritance”)它?

好吧,我能看到你在C#/ Java中实现这一目标的唯一方法是通过组合。 考虑一下:

class Foo { } interface A { public void a(); } interface B { public void b(); } class ImplA implements A { @Override public void a() { System.out.println("a"); } } class ImplB implements B { @Override public void b() { System.out.println("b"); } } class Bar extends Foo { A a = new ImplA(); public void a() { aa(); } } class Baz extends Foo { B b = new ImplB(); public void b() { bb(); } } class Qux extends Foo { A a = new ImplA(); B b = new ImplB(); public void b() { bb(); } public void a() { aa(); } } 

现在Qux通过正常inheritance既具有Foo的function,又通过组合实现AB的实现。

更一般的术语是Mixin 。 有些语言提供开箱即用的支持,例如Scala和D.但是有多种方法可以在其他语言中实现相同的结果。

在C#中创建伪mixin的一种方法是使用空接口并为方法提供扩展方法。

 interface A { } static class AMixin { public static void aFunc(this A inst) { ... //implementation to work for all A. } } interface B { } static class BMixin { public static void bFunc(this B inst) { ... } } class Qux : Foo, A, B { ... } 

这在提供特征的语言中是可以实现的(这里是: scala ):

 class Foo { def fooM() {} } trait A { def aFunc() {} } trait B { def bFunc() {} } class Bar extends Foo with A {} class Baz extends Foo with B {} class Qux extends Foo with A with B {} 

因为Scala运行在Java之上(既没有多重inheritance也没有特性),它被翻译成类似这样的东西(简化) – 这可能是一个提示如何在Java / C#中手动实现它:

 class Foo { } interface A { void aFunc(); } interface B { void bFunc(); } class Bar extends Foo implements A { public void aFunc() { $A.aFunc(); } } class Baz extends Foo implements B { public void bFunc() { $B.bFunc(); } } class Qux extends Foo implements A, B { public void aFunc() { $A.aFunc(); } public void bFunc() { $B.bFunc(); } } class $A { public static void aFunc() {} } class $B { public static void bFunc() {} } 

有几种方法可以做这样的事情。 更具体地说,如果我们暂时放弃inheritance方面,有一些方法可以将相同的function单元引入不同的类,而只需要编写一次单元。

好吧,我喜欢 AOP Frameworks,它们存在于许多语言中(C#和Java有几种语言)。 AOP框架基本上允许您在整个inheritance结构中将自包含function添加到不同的类中。

对于C#,你有PostSharp和Java,你有AspectJ ,以及其他许多。

许多AOP框架允许在不使用inheritance的情况下“劫持”或“覆盖”方法调用。