Java接口:继承、重写和重载方法
在“Java™ Ken Arnold、James Gosling、David Holmes编写的“编程语言,第四版”,其中提到: 段落:(4.3.2) 类似地,如果一个接口继承了多个具有相同签名的方法,或者如果一个类实现了包含具有相同签名的方法的不同接口,则只有一个这样的方法。此方法的实现最终由实现接口的类定义,并且没有歧义。如果s具有相同的签名但返回类型不同,则其中一个返回类型必须是所有其他返回类型的子类型,否则会发生编译时错误。实现必须定义一个返回该公共子类型的方法。” 有人能给我一些示例代码来证明上述段落的观点吗? 我试图编写代码并测试上面提到的内容,但我遇到了编译时错误 子接口隐藏基本接口方法,因此只能实现子接口方法 提前谢谢。 -ArunJava接口:继承、重写和重载方法,java,interface,overriding,overloading,Java,Interface,Overriding,Overloading,在“Java™ Ken Arnold、James Gosling、David Holmes编写的“编程语言,第四版”,其中提到: 段落:(4.3.2) 类似地,如果一个接口继承了多个具有相同签名的方法,或者如果一个类实现了包含具有相同签名的方法的不同接口,则只有一个这样的方法。此方法的实现最终由实现接口的类定义,并且没有歧义。如果s具有相同的签名但返回类型不同,则其中一个返回类型必须是所有其他返回类型的子类型,否则会发生编译时错误。实现必须定义一个返回该公共子类型的方法。” 有人能给我一些示例代
接口A
{
void foo();
//int-bar();
如您所见,Impl.method()
实现了A.method()
和B.method()
,而Impl.returnMethod()
返回一个B
,它是对象的子对象,从而实现了A.returnMethod()
的契约。后者是否要求返回类型不是B.returnMethod()
的返回类型的父级,这将是一个可笑的错误,因为在Impl
中不可能存在这样的实现。这是您的意思吗
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
/
/
/etc)。在以下两个接口中methodA()
在参数(none)和返回类型(int)方面的定义是相同的。底部的实现类定义了一个具有此精确签名的方法。由于它符合这两个接口,因此没有问题-通过类型InterfaceA或InterfaceB的引用进行的任何调用都将发送到此实现
第二个methodB()
定义为在InterfaceA
中返回Number
(或Number
本身)的任何子类型返回一个整数
,该整数是数字
的子类型。实现类实际使用整数
实现该方法,从而遵守接口a
和接口b
的约定。这里也没有问题。
注释掉的methodB()
被实现为返回一个Double
的情况将不起作用:虽然它将满足InterfaceA
的契约,但它将与InterfaceB
冲突(需要一个整数
)
如果InterfaceA
和InterfaceB
也为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;
// }
}
发布你的代码和编译器错误,伙计……至少给我们一些东西(a)重现问题;和(b)从那里开始。各位,我真的很抱歉-我试图使用j2sdk1.4.208测试上述段落中提到的内容-我没有意识到这本书是为JDK1.5编写的,因此这意味着如果您使用“Daniel Schneller”使用JDK1.4,您将在ImplementationOfAandB中得到一个“ImplementationOfAandB.java:17:methodB(),而在InterfaceAAndB中无法实现methodB();尝试使用不兼容的返回类型"编译错误,而JDK1.5运行正常。这是C#吗?问题是关于Java的。我认为哪种语言不重要;)而且不,Impl必须实现更具体的返回类型才能满足这两个接口。我真的很抱歉,我正试图用j2sdk1.4.208测试上述段落中提到的内容-我没有意识到这本书是为JDK1.5编写的,因此这意味着如果您使用JDK1.4编译“Daniel Schneller”编写的代码段,您将得到一个“Implementation of AndB.java:17:Implementation of AndB中的methodB()无法在InterfaceA中实现methodB();尝试使用不兼容的返回类型”编译错误,而JDK1.5运行正常。
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; }
}
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
}
}
}