繁体   English   中英

Java接口:继承,重写和重载方法

[英]Java Interface: Inheriting, Overriding, and Overloading Methods

在“The Java™Programming Language,Fourth Edition”中,Ken Arnold,James Gosling,David Holmes提到:

段落:(4.3.2) “同样,如果一个接口继承了多个具有相同签名的方法,或者一个类实现了包含具有相同签名的方法的不同接口,则只有一个这样的方法。该方法的实现最终由实现接口的类定义,并且没有歧义。如果方法具有相同的签名但返回类型不同,则其中一个返回类型必须是所有其他类型的子类型,否则编译时错误实现必须定义一个返回该公共子类型的方法。“

任何人都可以给我一些示例代码来证明上段的要点吗?

我试着编写代码并测试提到的但是我得到编译时错误,子接口隐藏了基本接口方法,所以只能实现子接口方法。

提前致谢。 -Arun

interface A {
    void method();
    Object returnMethod();
}
interface B {
    void method();
    B returnMethod();
}

class Impl implements A,B 
{
    void method() { }
    B returnMethod() { }
}

如您所见, Impl.method()实现了A.method()B.method() ,而Impl.returnMethod()返回一个B ,它是Object的子A.returnMethod() ,因此实现了A.returnMethod()的实现。合同也是。 后者是否需要一个不是B.returnMethod()返回类型的父类的返回类型,这将是一个comile错误,因为在Impl不存在这样的实现。

在以下两个接口中, methodA()在参数(none)和返回类型(int)方面相同地定义。 底部的实现类定义了具有此精确签名的单个方法。 由于它符合两个接口,因此您不会遇到任何问题 - 通过InterfaceA或InterfaceB类型的引用进行的任何调用都将被分派到此实现。

第二个方法methodB()被定义为在InterfaceA返回Number (或Number本身)的任何子类型。 InterfaceBmethodB()定义为返回一个Integer ,它是Number的子类型。 实现类实际上使用Integer实现该方法,因此遵守InterfaceAInterfaceB的约定。 这里也没问题。 注释掉的methodB()被实现为返回一个Double情况不会起作用:虽然它会满足InterfaceA的契约,但它会与InterfaceB (它需要一个Integer )冲突。

如果InterfaceAInterfaceB也为methodC() (在示例中注释掉)指定(不同的)契约,那么这将是矛盾的并且会产生编译器错误。 在Java中不允许实现两个签名(仅在返回类型上不同)。

如果要向方法添加任何参数,上述规则也适用。 为简单起见,我保留了这个例子。

public interface InterfaceA {
    public int methodA();
    public Number methodB();
    // public int methodC(); // conflicting return type
}

public interface InterfaceB {
    public int methodA();
    public Integer methodB();
    // public String methodC(); // conflicting return type
}

public class ImplementationOfAandB implements InterfaceA, InterfaceB {
    public int methodA() {
        return 0;
    }
    public Integer methodB() {
        return null;
    }
    // This would NOT work:
    // public Double methodB() {
    //     return null;
    // }
}
/**
 * This is what you have
 */
interface IXR {
        //bla-bla-bla
}

class CXR implements IXR {
        //concrete implementation of bla-bla-bla
}

interface IX {
        public IXR f();
} 

interface IYR {
        //some other bla-bla-bla
}

class CYR implements IYR {
        //concrete implementation of the some other bla-bla-bla
} 

interface IY {
        public IYR f();
}






/**
 * This is what you need to add
 */ 
interface IZR extends IXR, IYR {
        //EMPTY INTERFACE
}

class CZXR extends CXR implements IZR {
        //EMPTY CLASS
} 

class CZYR extends CYR implements IZR {
        //EMPTY CLASS
}

class CZ implements IX, IY
{
        public static boolean someCondition = true;

        public IXR implementationOf_X_f()
        {
                System.out.println("CXR");
                return new CZXR();
        }

        public IYR implementationOf_Y_f()
        {
                System.out.println("CYR");
                return new CZYR();
        }

        public IZR f() {
                if (someCondition) {
                        return (IZR) implementationOf_X_f();
                } else {
                        return (IZR) implementationOf_Y_f();
                }
        }

}






/**
 * This is the usage of the required class
 */
class program 
{ 
        public static void main(String[] x) {
                CZ o = new CZ();
                IZR r = o.f();
                if (CZ.someCondition) {
                        CXR xr = (CXR) r;
                        //bla-bla-bla
                } else {
                        CYR yr = (CYR) r;
                        //bla-bla-bla
                }
        }
} /**
 * This is what you have
 */
interface IXR {
        //bla-bla-bla
}

class CXR implements IXR {
        //concrete implementation of bla-bla-bla
}

interface IX {
        public IXR f();
} 

interface IYR {
        //some other bla-bla-bla
}

class CYR implements IYR {
        //concrete implementation of the some other bla-bla-bla
} 

interface IY {
        public IYR f();
}






/**
 * This is what you need to add
 */ 
interface IZR extends IXR, IYR {
        //EMPTY INTERFACE
}

class CZXR extends CXR implements IZR {
        //EMPTY CLASS
} 

class CZYR extends CYR implements IZR {
        //EMPTY CLASS
}

class CZ implements IX, IY
{
        public static boolean someCondition = true;

        public IXR implementationOf_X_f()
        {
                System.out.println("CXR");
                return new CZXR();
        }

        public IYR implementationOf_Y_f()
        {
                System.out.println("CYR");
                return new CZYR();
        }

        public IZR f() {
                if (someCondition) {
                        return (IZR) implementationOf_X_f();
                } else {
                        return (IZR) implementationOf_Y_f();
                }
        }

}






/**
 * This is the usage of the required class
 */
class program 
{ 
        public static void main(String[] x) {
                CZ o = new CZ();
                IZR r = o.f();
                if (CZ.someCondition) {
                        CXR xr = (CXR) r;
                        //bla-bla-bla
                } else {
                        CYR yr = (CYR) r;
                        //bla-bla-bla
                }
        }
}
interface A
{
   void foo();
   //int bar(); <-- conflicts with B.bar() because of different return type
}

interface B
{
   void foo();
   //double bar(); <-- conflicts with A.bar() because of different return type
}

class C implements A, B
{
   void foo() // this implements A.foo() AND B.foo()
   {
      ...
   }
}

你是这个意思吗?:

interface A {
    Object get();
}
interface B {
    Number get();
}

abstract class MyClass implements A, B {
    // Try to override A.get, but cause a compile error.
    public Object get() { return null; }
}

MyClass中的这种方法由javac自动生成为合成桥接方法。 您必须实现一个返回与所有已实现/重写的方法兼容的类型的方法(在本例中为Number / Integer / Double / etc)。

暂无
暂无

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

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