-1

似乎 Java 8 允许使用如下简单框架实现完全成熟的继承,在接口上使用静态和默认方法。

虽然总是有可能误用和编写愚蠢的代码,但这些新特性使得实现多重继承变得相当容易,即使语言的设计者打算远离它。

这是使用接口作为基类的多重继承的简单实现,还是对语言的滥用?Java 设计者是否允许这样做太过分了?

package PseudoMultipleInheritance;
import java.util.HashMap;

abstract class InstanceMap<T,T2>
{ 
     HashMap<Object,Object> instances = new HashMap<Object, Object>();
     abstract T2 createMembersInstance();
     T2 getMembersInstance(T thisObject )
     {
         if ( !instances.containsKey(thisObject) )
             instances.put(thisObject,createMembersInstance());
         return (T2) instances.get(thisObject);      
     }  
}

interface A  
{
 class Members
 {
    int x;  // just an example of an inheritable member 
 }
 InstanceMap<A,A.Members> instanceMap = new InstanceMap<A, A.Members>() {   A.Members createMembersInstance() {return new A.Members();  }};
 default A.Members thisA() { return instanceMap.getMembersInstance(this); }
 default int getX()
 {
     return thisA().x;  // // just an example of an inheritable getter
 }
 default void setX(int x)
 {
     thisA().x = x;  // just an example of an inheritable setter
 }
}

interface B 
{
 class Members
 {
    int y;  // just an example of an inheritable member
 }  
 InstanceMap<B,B.Members> instanceMap = new InstanceMap<B, B.Members>() {   B.Members createMembersInstance() {return new B.Members();} };  
 default B.Members thisB() { return instanceMap.getMembersInstance(this); }
 default int getYLastDigit()
 {
     return thisB().y % 10;  // just an example of an inheritable function
 }
 default void incrementY(int x)
 {
     thisB().y += x; // just an example of an inheritable function
 }
}

class C implements A, B
{
}

public class Example04AlmostMultipleInheritance {

 public static void main(String[] args) {
     C c1 = new C();
     C c2 = new C();
     c1.setX(5);
     c2.setX(3);
     System.out.println(c1.getX());   // prints 5
     System.out.println(c2.getX());   // prints 3
     c1.incrementY(99);
     System.out.println(c1.getYLastDigit());  // prints 9 

}
}

///////////////////////////////////////// /

或者另一种选择:

interface A  
{
 class Members
 {
    public int x;  // just an example of an inheritable member
    void showX() { System.out.println(x); }  // just an example of an inheritable function
 }
 InstanceMap<A,A.Members> instanceMap = new InstanceMap<A, A.Members>() {   A.Members createMembersInstance() {return new A.Members();  }};
 default A.Members getA() { return instanceMap.getMembersInstance(this); }
}

interface B 
{
 class Members
 {
    int y;  // just an example of an inheritable member
 }  
 InstanceMap<B,B.Members> instanceMap = new InstanceMap<B, B.Members>() {   B.Members createMembersInstance() {return new B.Members();} };  
 default B.Members getB() { return instanceMap.getMembersInstance(this); }
}

class C implements A, B
{
}

public class Example04AlmostMultipleInheritance {

 public static void main(String[] args) {
     C c1 = new C();
     C c2 = new C();
     c1.getA().x = 5;
     c2.getA().x = 3;
     c1.getA().showX();   // prints 5
     c2.getA().showX();   // prints 3
     c1.getB().y = 99;
     System.out.println(c1.getB().y % 10);  // prints 9 

}
}
4

3 回答 3

1

Implementing an interface is not inheritance. It's as simple as that.

what if someone implements both your interfaces but supplies a different implementation than the default one?

So no, this is not multiple inheritance, It's simply a way to write your code that depends on nobody actually implementing their own version of the default methods. Which means that it depends on people not actually using interfaces the way they're supposed to, because they are supposed to be able to implement their own methods instead of the defaults, but if they actually do that your "multiple inheritance" does not work as expected.

So I'd actually consider this to be misuse of the language.

于 2014-10-08T23:17:17.720 回答
1

它是多重继承的一种形式,但并不是讨论多重继承时通常会提到的“钻石问题”。Java 的实现与 Scala 对同一事物的解决方案几乎相同,这与 Python 实现多重继承的方式有些相似。

于 2014-10-08T23:27:16.313 回答
0

不,请参见此处的多重继承示例:http: //java.dzone.com/articles/interface-default-methods-java

于 2014-10-09T00:44:59.290 回答