[英]Java design issue: Inheriting from multiple abstract classes
I have a design problem in Java. 我在Java中遇到设计问题。 I have 我有
Feature
which represents features (machine learning), and 代表Feature
(机器学习)的抽象类Feature
,以及 Normalizable
which inherits from Feature
and represents features which can be normalized. 从Feature
继承并表示可以规范化的Feature
的抽象类Normalizable
。 A concrete feature can be an instance of either Feature
or Normalizable
. 具体要素可以是Feature
或Normalizable
的实例。
I would like to introduce an abstract class called Probablity
which inherits from Feature
and represents features on which some sort of probability calculations can be done. 我想介绍一个称为Probablity
的抽象类,该类继承自Feature
并表示可以进行某种概率计算的特征。
An actual feature should either be an instance of Feature
(not normalizable and no probability) or it should be an instance of Normalizable
, or Probability
or both. 实际要素应该是Feature
的实例(不可归一化且没有概率),或者应该是Normalizable
,或Probability
或两者兼有的实例。
The problem now is that an actual feature cannot inherit from both Normalizable
and Probability
. 现在的问题是,实际功能不能同时继承Normalizable
和Probability
。
How can I solve this issue? 我该如何解决这个问题? I think using interfaces is not the solution because all of the mentioned classes contains implementations. 我认为使用接口不是解决方案,因为所有提到的类都包含实现。
You can create a class NormalizablePrbability.java
Use Composition
and have Probability.java
and Normalize.java
as fields in newly created class NormalizablePrbability.java
. 你可以创建一个类NormalizablePrbability.java
使用Composition
,并有Probability.java
和Normalize.java
在新创建的类字段NormalizablePrbability.java
。 You can extend NormalizablePrbability.java
from Feature.java
and override the methods and in each method you can provide the combined features of both Probability.java
and Normalize.java
. 您可以从Feature.java
扩展NormalizablePrbability.java
并覆盖方法,并且在每种方法中,您都可以提供Probability.java
和Normalize.java
的组合功能。
Now you can have your concrete classes which can extends from Feature.java
or Normalize.java
or Probability.java
or NormalizablePrbability.java
现在,您可以拥有可以从Feature.java
或Normalize.java
或Probability.java
或NormalizablePrbability.java
扩展的具体类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.