English 中文(简体)
多重继承,没有多重继承,没有法典重复
原标题:Multiple inheritance without multiple inheritance and without code duplication

我有一个理论问题,涉及如何用不允许多重继承的语言处理以下情形。

试想一下,我有一个基础级 Foo ,我想从中创建三个子级:

  • Class Bar inherits Foo and implements functionality "A"
  • Class Baz inherits Foo and implements functionality "B"
  • Class Qux inherits Foo and implements functionalities "A" and "B"

想象执行功能“ A” 和“ B” 的代码总是相同的。 是否有方法只写一次“ A” 和“ B” 的代码, 然后再使用合适的分类( 或“ 继承 ” )?

最佳回答

在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() {
        a.a();
    }
}

class Baz extends Foo {
    B b = new ImplB();

    public void b() {
        b.b();
    }       
}

class Qux extends Foo {

    A a = new ImplA();
    B b = new ImplB();

    public void b() {
        b.b();
    }

    public void a() {
        a.a();          
    }       
}

现在 ux 既具有通过正常继承 Foo 的功能,也具有根据组成实施 A B 的功能。

问题回答

更一般的术语是 < a href=" "http://en.wikipedia.org/wiki/Mixin" rel="nofollow" >Mixin 。有些语言在框外提供支持,例如Scala和D。 不过,其他语言也有不同的方式取得同样的结果。

您可以在 C# 中创建假混血素的方法之一是使用空界面并提供扩展方法的方法 。

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 的标有 scala “ rel='tag' > 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跑在爪哇山顶(既无多个遗产,也无特征),

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() {}
}

有几种方法可以做这样的事情。更具体地说,如果我们暂时放弃继承问题,就会有办法在不同阶层引入相同的功能单位,而只写一次。

好吧,我love AOP 框架,它有多种语言(C#和Java有几种语言)。AOP框架基本上允许您在整个继承结构中将自足功能添加到不同的类别中。

对于C#,您有"http://www.sharrpcrafters.com/"rel="nofollow" >PostSharp ,对于爪哇,您有AspectJ ,等等。

许多AOP框架允许在不使用继承的情况下劫持或压倒使用方法的电话。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签