java - Java中的多重继承设计问题

标签 java inheritance multiple-inheritance

你如何处理 java 中只有单一继承的问题?这是我的具体问题:

我有三个(简化的)类:

public abstract class AbstractWord{
    String kind;    // eg noun, verb, etc

    public String getKind(){ return kind; }

}

public class Word extends AbstractWord{
    public final String word;

    ctor...

    public void setKind(){
        // based on the variable word calculate kind..
    }
}

public class WordDescriptor extends AbstractWord{

    ctor..

    public void setKind(String kind){this.kind = kind;}

}

这是我认为最基本的实现,但我想进行其他实现。

假设我想添加一个新变量 wordLength,但我想使用继承来添加它。意思是我不想修改原来的 AbstractWord 类。即类似这样的内容:

public class Length{
    private int length;

    public int getLength(){return length};
}

public class BetterWord extends AbstractWord AND Length{

    public void setLength(){
        // based on the variable word calculate Length..
    }
}

public class BetterWordDescriptor extends AbstractWord AND length{

    public void setLength(int length){this.length = length;}
}

我知道 java 不允许我这样做,但它使我的代码非常难看。现在,每当我添加一个字段时,我只是将它添加到 AbstractWord,但随后我要么需要重命名该 AbstractWord(以及 Word 和 WordDescriptor)。 (由于向后兼容性,我不能只将字段添加到另一个字段,它会破坏 equals 方法和类似的东西)。

这似乎是一个很常见的设计问题,但我绞尽脑汁也想不出任何漂亮的解决方案。

是否有解决此问题的设计模式?我有一些潜在的解决方案,但我想看看我是否遗漏了什么。

谢谢, jack

更新:长度指的是单词中的音节数(抱歉不清楚)

最佳答案

支持组合而不是继承。

解决方案考虑到可能有另一种类型的单词可能需要 WordLengthSupport。

类似地,可以创建和实现其他接口(interface),各种词类型可以混合和匹配这些接口(interface)。

.

public class WordLength {
    private int length = 0;
    public int getLength(){return length};
    public void setLength(int length){this.length = length};
}

.

public interface WordLengthSupport {
    public WordLength getWordLength();
}

.

public class BetterWord extends AbstractWord 
        implements WordLengthSupport {
    WordLength wordLength;
    public WordLength getWordLength() {
        if(wordLength==null) {
            // each time word changes 
            // make sure to set wordLength to null
            calculateWordLength(); 
        }
        return wordLength;
    }
    private void calculateWordLength() {
        // This method should be 
        //    called in constructor 
        //    or each time word changes 
        int length = // based on the variable word calculate Length..
        this.wordLength = new WordLength();
        this.wordLength.setLength(length);
    }
}

.

public class BetterWordDescriptor extends AbstractWord  
        implements WordLengthSupport {
    WordLength wordLength;
    public WordLength getWordLength(return wordLength);
    public void setWordLength(WordLength wordLength) {
        // Use this to populate WordLength of respective word
        this.wordLength = wordLength;
    }
}

.

策略模式定义了一系列算法,封装了每一个算法,并使它们可以互换。策略让算法独立于使用它的客户而变化。

此解决方案不使用策略模式,但可以对其进行重构。

关于java - Java中的多重继承设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2143220/

相关文章:

c++ - 被简单的 C++ 多重继承示例难住了

java - jfreechart需要自定义Y轴只是为了打印

Java : "xx". equals(variable) 比 variable.equals ("xx") 好,真的吗?

java - 将所选元素移动一位并返回其更新后的索引

java - 使用 LocalDate 作为 POJO 的数据类型时,为什么 univocity 的 CsvParser 会抛出错误以及如何解决它?

c# - 如何让子类实现接口(interface)?

python - 从方法中确定定义类

java - opencsv不导出父类的属性

ios - IOS 的多重继承

c++ - 为什么 const/non-const 函数重载的继承不明确?