我们是否需要将接口方法声明为抽象方法
可能的重复:
Java抽象接口
public interface Foo {
abstract public void bar();
}
我想我们也不需要声明 abstract
在上面的接口
中作为公共。编译器会捕获这是作为警告还是编译器允许。
Possible Duplicate:
Java abstract interface
public interface Foo {
abstract public void bar();
}
I guess we don't need to declare abstract
as well as public in the above interface
. Will the compiler catch this is as a warning or is it allowed by the compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在接口中,修饰符
public
和abstract
隐含在方法中,与字段public
static
和类似。 >final
是隐含的。对于内部类来说,static
是隐含的。In an interface the the modifiers
public
andabstract
are implied for methods, similarly for fieldspublic
static
andfinal
are implied. For inner classesstatic
is implied.这是允许的。
public
和abstract
会自动添加到每个interface
方法中。It is allowed.
public
andabstract
are automatically added to everyinterface
method.您不必这样做,每个接口方法都是隐式抽象的。不过这样写也不会出错。
You don't have to, every interface method is implicitly
abstract
. It will not be a mistake to write it though.对于接口方法,不需要声明public和abstract
默认情况下,这些是公共和抽象的
For interface methods, it is not necessary to declare public and abstract
by default those are public and abstract
虽然没有必要,但是写下来也没什么坏处。这些修饰符是隐含的。
我喜欢这样做,这样一切都是明确的,并且可以帮助其他将使用您的代码的程序员。
It is not necessary but it won't hurt to write it. These modifiers are implied.
I like to do it so everything is explicit and may help other programmers that will work with your code.
您可以在接口内声明抽象。编译器可以通过它。
但没有必要抽象地声明,因为
我们不会实现或允许实现接口内部的方法。
You are allowed to declare abstract inside the interface. The complier can pass it.
But there is no point to declare in abstract since
we would not implement or allow to implement methods inside interface.