java – Interface中的内部类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Interface中的内部类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1345字,纯文字阅读大概需要2分钟。
内容图文
![java – Interface中的内部类](/upload/InfoBanner/zyjiaocheng/744/04bffe2378054f2cb95c5943afa1e922.jpg)
我们可以在接口内部有一个类,该接口具有不同的接口实现方法.我在这里有一个疑问,为什么Java允许在接口内部编写内部类,以及我们可以在哪里使用它.
在下面的程序中,我在Interface中编写了一个类,并实现了接口的方法.在接口的实现类中,我刚刚调用了内部类方法.
public interface StrangeInterface
{
int a=10;int b=5;
void add();
void sub();
class Inner
{
void add()
{
int c=a+b;
System.out.println("After Addition:"+c);
}
void sub()
{
int c=a-b;
System.out.println("After Subtraction:"+c);
}
}
}
abstract public class StrangeInterfaceImpl implements I {
public static void main(String args[])
{
StrangInterface.Inner i=new StrangeInterface.Inner();
i.add();
i.sub();
}
}
解决方法:
您可以在接口内定义类.在接口内部,内部类是隐式的公共静态.
The body of an interface may declare members of the interface, that is, fields (§9.3), methods (§9.4), classes (§9.5), and interfaces (§9.5).
从JLS Section 9.5开始:
Interfaces may contain member type declarations (§8.5).
A member type declaration in an interface is implicitly static and public. It is permitted to redundantly specify either or both of these modifiers.
对于在接口或任何其他类中定义的内部类的唯一限制是,您必须使用封闭的成员名称来访问它们.
除此之外,他们之间没有任何关系.编译后,内部类将导致完全不同的类文件.
例如,如果您编译以下源文件:
interface Hello {
class HelloInner {
}
}
将生成两个类文件:
Hello.class
Hello$HelloInner.class
内容总结
以上是互联网集市为您收集整理的java – Interface中的内部类全部内容,希望文章能够帮你解决java – Interface中的内部类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。