本文介绍了Interface和Abstract类中的方法相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了这种情况:
public interface Intr {
public void m1();
}
public abstract class Abs {
public void m1() {
System.out.println("Abs.m1()");
}
// public abstract void m1();
}
public class A extends Abs implements Intr {
@Override
public void m1() {
// which method am I overriding, well it is Abs.m1() but why?
// if method implemented is Abs.m1(), then why I am not getting error for Intr.m1() not implemented.
}
}
推荐答案
您同时满足这两个条件;即。一个实现同时满足抽象类要求和接口要求。
You are satisfying both conditions at once; ie. the one implementation is at the same time fulfilling the abstract class requirements and the interface requirements.
作为注释,除非您使用 Intr
在另一个继承链中,你不需要它。此外,将实现Intr
移动到抽象类定义可能是有意义的。
As a note, unless you are using Intr
in another inheritance chain, you don't need it. Also, it might make sense to move the implements Intr
up to the abstract class definition.
这篇关于Interface和Abstract类中的方法相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!