我是靠谱客的博主 活力蜜蜂,这篇文章主要介绍静态内部类和普通内部类的区别以及外部类对其访问方式,现在分享给大家,希望可以做个参考。

区别:
静态内部类相对与外部类是独立存在的,在静态内部类中无法直接访问外部类中变量、方法。如果要访问的话,必须要new一个外部类的对象,使用new出来的对象来访问。但是可以直接访问静态的变量、调用静态的方法;
普通内部类作为外部类一个成员而存在,在普通内部类中可以直接访问外部类属性,调用外部类的方法。
外部类访问方式:
public class test {

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int c=1; static class hah { int a=1; public int get_a() { int d=new test().c; return a+d; } } class heh { int b =1; public int get_b() { return b; } }

}

public class robt {

复制代码
1
2
3
4
5
6
7
8
9
public static void main(String [] args) { //静态内部类直接new test.hah h=new test.hah(); int c=h.get_a(); //非静态内部类要通过自身外部类的类明调用 test.heh d=new test().new heh(); }

}

最后

以上就是活力蜜蜂最近收集整理的关于静态内部类和普通内部类的区别以及外部类对其访问方式的全部内容,更多相关静态内部类和普通内部类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部