English 中文(简体)
采用接口的 Java“多重遗产”——只避免公共方法
原标题:Java "multiple inheritance" using interfaces - avoid only public methods

我的目标是在 Java拥有某种类似钻石的遗产。 与此类似:

“enterography

我知道, Java不可能有真正的多重遗产。 这并不是我的实际问题。

大家都提到的解决办法是使用接口。 这样做,我可以这样做,但这一遗产将成为APIC的一部分。 因此,一切都不应公开,也不应对外开放。 但想的是:所有接口必须公开。

If you take a look at the graph again, this describes my problem further: I want to declare a method in B that gets implemented in A. However, from outside of this diamond you should not be able to access this method. My interface B needs data in a default-implemented method that it can only get from this method that A has to implement. However, from outside you should not be able to call it.

我也知道,许多人说,接口只应具体说明行为。 我采用我的缺省执行方法,在一定程度上打破了这一公约。 我可以想象,这是我首先 st倒这些问题的原因。

现在我的问题是: 我是否犯了根本的结构错误? 如果是的话,我能避免这种情况? 是否有其他办法实现这种功能?

一天:

问题回答

if you are using the java 17 version or later, You can use the sealed interfaces and classes so that you can control which classes that could implement an interface (for example you can create the interface B and permit only the class A to implement the interface B)

public sealed interface B permits A {
  // your code
}

允许的子类(A类)可宣布为最后级,以防止任何进一步延长。

public final class A implements B {
  // your code
}




相关问题
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 ...

热门标签