简体   繁体   English

Java设计问题:从多个抽象类继承

[英]Java design issue: Inheriting from multiple abstract classes

I have a design problem in Java. 我在Java中遇到设计问题。 I have 我有

  • an abstract class Feature which represents features (machine learning), and 代表Feature (机器学习)的抽象类Feature ,以及
  • an abstract class 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 . 具体要素可以是FeatureNormalizable的实例。

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 . 现在的问题是,实际功能不能同时继承NormalizableProbability

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.javaNormalize.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.javaNormalize.java的组合功能。

Now you can have your concrete classes which can extends from Feature.java or Normalize.java or Probability.java or NormalizablePrbability.java 现在,您可以拥有可以从Feature.javaNormalize.javaProbability.javaNormalizablePrbability.java扩展的具体类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM