概述
区别:
静态内部类相对与外部类是独立存在的,在静态内部类中无法直接访问外部类中变量、方法。如果要访问的话,必须要new一个外部类的对象,使用new出来的对象来访问。但是可以直接访问静态的变量、调用静态的方法;
普通内部类作为外部类一个成员而存在,在普通内部类中可以直接访问外部类属性,调用外部类的方法。
外部类访问方式:
public class test {
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 {
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();
}
}
最后
以上就是活力蜜蜂为你收集整理的静态内部类和普通内部类的区别以及外部类对其访问方式的全部内容,希望文章能够帮你解决静态内部类和普通内部类的区别以及外部类对其访问方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复