内部类顾名思义就是定义在类中的类,下面做一个简单介绍:

内部类的访问规则:
1,内部类可以直接访问外部类中的成员,包括私有。
之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式 外部类名.this
2,外部类要访问内部类,必须建立内部类对象。

class Outer
{
private int x = 3; class Inner//内部类
{
//int x = 4;
void function()
{
//int x = 6;
System.out.println("innner :"+Outer.this.x);
}
} /**/
void method()
{
Inner in = new Inner();
in.function();
}
} class InnerClassDemo
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method(); //直接访问内部类中的成员。
// Outer.Inner in = new Outer().new Inner();
// in.function();
}
}

  

访问格式:
1,当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中。
可以直接建立内部类对象。
格式
外部类名.内部类名 变量名 = 外部类对象.内部类对象;
Outer.Inner in = new Outer().new Inner();

2,当内部类在成员位置上,就可以被成员修饰符所修饰。
比如,private:将内部类在外部类中进行封装。
static:内部类就具备static的特性。
当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。

在外部其他类中,如何直接访问static内部类的非静态成员呢?
new Outer.Inner().function();

在外部其他类中,如何直接访问static内部类的静态成员呢?
uter.Inner.function();

注意:当内部类中定义了静态成员,该内部类必须是static的。
当外部类中的静态方法访问内部类时,内部类也必须是static的。

当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事务在使用外部事物的内容。

class Outer
{
private static int x = 3; static class Inner//静态内部类
{
static void function()
{
System.out.println("innner :"+x);
}
} static class Inner2
{
void show()
{
System.out.println("inner2 show");
}
} public static void method()
{
//Inner.function();
new Inner2().show();
} } class InnerClassDemo2
{
public static void main(String[] args)
{
Outer.method();
//Outer.Inner.function();
//new Outer.Inner().function();
//直接访问内部类中的成员。
// Outer.Inner in = new Outer().new Inner();
// in.function();
}
}

  

内部类定义在局部时,也就是定义在类的成员函数中。
1,不可以被成员修饰符修饰
2,可以直接访问外部类中的成员,因为还持有外部类中的引用。
但是不可以访问它所在的局部中的变量。只能访问被final修饰的局部变量。

class Outer
{
int x = 3; void method(final int a)
{
final int y = 4;
class Inner
{
void function()
{
System.out.println(y);
}
} new Inner().function(); }
} class InnerClassDemo3
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method(7);
out.method(8);
} }

匿名内部类:
1,匿名内部类其实就是内部类的简写格式。
2,定义匿名内部类的前提:
内部类必须是继承一个类或者实现接口。
3,匿名内部类的格式: new 父类或者接口(){定义子类的内容}
4,其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖。 可以理解为带内容的对象。
5,匿名内部类中定义的方法最好不要超过3个。

abstract class AbsDemo
{
abstract void show(); } class Outer
{
int x = 3; /*
class Inner extends AbsDemo
{
int num = 90;
void show()
{
System.out.println("show :"+num);
}
void abc()
{
System.out.println("hehe");
}
}
*/ public void function()
{
//AbsDemo a = new Inner();
// Inner in = new Inner();
// in.show();
// in.abc(); AbsDemo d = new AbsDemo()
{
int num = 9;
void show()
{
System.out.println("num==="+num);
}
void abc()
{
System.out.println("haha");
}
}; d.show();
//d.abc();//编译失败; }
} class InnerClassDemo4
{
public static void main(String[] args)
{
new Outer().function();
}
}

  

最新文章

  1. [moka同学收藏]网页上的“返回上一页”的几种实现代码
  2. 一个静态的HTML页面用jquery ajax登录到sharepoint页面
  3. 【Java环境变量的配置问题】
  4. ios——MPMoviePlayerController截取视频缩略图 播放视频又可以截取视频缩略图
  5. Codeforces Round #313 (Div. 2)B.B. Gerald is into Art
  6. JSON的转换(将JSON转换为字符串,将字符串转化为JSON)
  7. MyBatis的分页操作(MySQL)
  8. .Net程序员快速学习安卓开发-布局和点击事件的写法
  9. JDBC的使用流程
  10. c++-STL:删除子串
  11. 了解Python列表的一些方法
  12. GO语言的包
  13. [leetcode]37. Sudoku Solver 解数独
  14. java.io.Serializable 序列化问题【原】
  15. 使用jQuery实现图片懒加载原理
  16. Android java 多线程(三)
  17. 除掉inline-block 间距
  18. 将 Unity5.3 的老项目升级到 Unity 2018.3 遇到的些许问题。
  19. go chapter 5 - 异常处理 error、panic、recover
  20. spring in action 学习笔记六:bean在不同情况下的默认id号或者将名字

热门文章

  1. NPOI 数据导入导出
  2. ckedit 图片上传 php
  3. 1、MVC和EF中的 Model First 和 Code First
  4. 【Mocha.js 101】钩子函数
  5. 一起来学习DOJO吧--序
  6. iOS开发 首次启动显示用户引导,第二次启动直接进入App,UIScrollView,UIPageControl,NSUserDefaults
  7. iredmail安装脚本分析(二)---get_all.sh 文件所在目录为PKGS
  8. B - Dividing
  9. 关于IIS服务器证书续订
  10. 当我们说线程安全时,到底在说什么——Java进阶系列(二)