内部类、匿名内部类

布满荆棘的人生 2021-12-24 08:29 500阅读 0赞

内部类、匿名内部类

1.内部类

概述:把类定义在其他类的内部,这个类就被称为内部类

内部类访问特点

1.内部类可以直接访问外部类成员,包括私有的

2.外部类要访问内部类成员,需先创建对象

  1. public class Outer {
  2. int num = 10;
  3. private int a = 100;
  4. //定义成员内部类
  5. class Inner {
  6. int b = 109;
  7. public void innerShow() {
  8. System.out.println("内部类的show方法");
  9. }
  10. //内部类可以直接访问外部类的成员,包括私有
  11. public void innerTest(){
  12. System.out.println(num);
  13. System.out.println(a);
  14. outerShow();
  15. outerTest();
  16. }
  17. }
  18. public void outerShow() {
  19. System.out.println("这是外部类的show方法");
  20. }
  21. private void outerTest() {
  22. System.out.println("这是外部类的show方法");
  23. }
  24. //外部类,想要访问内部类的成员,得创建内部类的对象
  25. public void method(){
  26. //创建内部类的对象
  27. Inner inner = new Inner();
  28. System.out.println(inner.b);
  29. inner.innerShow();
  30. }
  31. }
  32. //测试类
  33. public class MyTest {
  34. public static void main(String[] args) {
  35. Outer outer = new Outer();
  36. System.out.println(outer.num);
  37. outer.outerShow();
  38. System.out.println("----------------");
  39. //使用成员内部类的属性和方法
  40. //创建成员内部类的语法
  41. Outer.Inner inner=new Outer().new Inner();
  42. System.out.println(inner.b);
  43. inner.innerShow();
  44. System.out.println("---------------");
  45. outer.method();
  46. }
  47. }
  48. //输出
  49. 10

内部类分类

成员位置:在成员位置定义的类,被称为成员内部类
局部位置:在局部位置定义的类,被称为局部内部类
四、成员内部类

访问格式:外部类名.内部类名 变量名 = new 外部类名().new 内部类名();

  1. 案例
  2. //定义类
  3. class Body {
  4. //外部类,身体
  5. private boolean life= true;
  6. //生命状态
  7. public class Heart {
  8. //内部类,心脏
  9. public void jump() {
  10. System.out.println("心脏噗通噗通的跳")
  11. System.out.println("生命状态" + life); //访问外部类成员变量
  12. }
  13. }
  14. }
  15. //访问内部类
  16. public static void main(String[] args) {
  17. //创建内部类对象
  18. Body.Heart bh = new Body().new Heart();
  19. //调用内部类中的方法
  20. bh.jump();
  21. }

2.成员内部类的修饰符

1.private 为了保证数据的安全性
2.static 为了方便访问数据

注意:静态内部类访问的外部类数据必须用静态修饰,成员方法可以是静态的也可以是非静态的

面试题

  1. class Outer {
  2. public int num = 10;
  3. class Inner {
  4. public int num = 20;
  5. public void show() {
  6. int num = 30;
  7. System.out.println(num); //30
  8. System.out.println(this.num); //20
  9. System.out.println(new Outer().num); //10
  10. }
  11. }
  12. }
  13. class InnerClassTest {
  14. public static void main(String[] args) {
  15. Outer.Inner oi = new Outer().new Inner();
  16. oi.show();
  17. }
  18. }

3、局部内部类

局部内部类,定义在外部类方法中的局部位置。与访问方法中的局部变量相似,可通过调用方法进行访问

定义格式
class 外部类 {
修饰符 返回值类型 方法名(参数) {
class 内部类 {
//其他代码
}
}
}
访问方式

在外部类方法中,创建内部类对象,进行访问

  1. 案例
  2. public class MyTest {
  3. public static void main(String[] args) {
  4. }
  5. }
  6. class Wai {
  7. int b = 100;//成员变量
  8. public void show(final int a) {
  9. //局部变量
  10. final int num = 10;
  11. class Nei {
  12. public void neiShow() {
  13. //局部内部类,访问外部类的局部变量,局部变量必须加上final修饰,JDK1.8 默认就加上了
  14. System.out.println(num);
  15. System.out.println(a);
  16. }
  17. }
  18. //必须在外部类方法中创建内部类对象调用
  19. Nei n = new Nei();
  20. n.neiShow();
  21. }
  22. }
  23. //输出:
  24. 10
  25. 123

4、匿名内部类

匿名内部类的作用:临时定义某一指定类型的子类 ,定义后即刻创建刚刚定义的这个子类的对象

格式:

new 父类或接口(){

//进行方法重写

};

案例

  1. public static void main(String[] args) {
  2. //匿名内部类:是局部内部类的简写
  3. //匿名内部类,本质上是一个对象,是谁的对象,是实现了该接口或继承了该抽象类的子类对象
  4. new AA(){
  5. @Override
  6. public void show() {
  7. System.out.println("重写了show方法");
  8. }
  9. }.show();
  10. }
  11. }
  12. abstract class AA{
  13. public abstract void show();
  14. }
  15. //输出:重写了show方法

匿名内部类中this关键字

  1. //要求在控制台输出”HelloWorld”
  2. interface Inter {
  3. public static final int a = 23 ;
  4. }
  5. public class Test {
  6. public static void main(String[] args) {
  7. new Inter() {
  8. public void show() {
  9. // 这个this表示的是匿名内部类的这个对象
  10. System.out.println(this.a);
  11. }
  12. }.show();
  13. }
  14. }//输出:23
  15. 匿名内部类面试题interface Inter1 {
  16. void show();
  17. }
  18. class Outer {
  19. //补齐代码
  20. //返回值是一个对象
  21. public static Inter1 method(){
  22. Inter1 in = new Inter1() {
  23. @Override
  24. public void show() {
  25. System.out.println("Helloworld");
  26. }
  27. };
  28. return in;
  29. }
  30. }
  31. class OuterDemo {
  32. public static void main(String[] args) {
  33. //链式代码需返回值是一个对象
  34. Outer.method().show();
  35. }
  36. }

5、类中定义接口

  1. public class MyTest {
  2. public static void main(String[] args) {
  3. //间接方式
  4. Outer outer = new Outer();
  5. outer.waiShow();
  6. //直接方式
  7. new Outer.Inner() {
  8. @Override
  9. public void show() {
  10. System.out.println("重写了接口中的show方法2");
  11. }
  12. }.show();
  13. Outer.Inner inner= new Outer.Inner() {
  14. @Override
  15. public void show() {
  16. System.out.println("重写了接口中的show方法3");
  17. }
  18. };
  19. inner.show();
  20. }
  21. }
  22. class Outer{
  23. //成员内部接口
  24. interface Inner{
  25. void show();
  26. }
  27. public void waiShow(){
  28. new Inner(){
  29. @Override
  30. public void show() {
  31. System.out.println("重写了接口中的show方法1");
  32. }
  33. }.show();
  34. }
  35. }
  36. //输出:
  37. 重写了接口中的show方法1
  38. 重写了接口中的show方法2
  39. 重写了接口中的show方法3

发表评论

表情:
评论列表 (有 0 条评论,500人围观)

还没有评论,来说两句吧...

相关阅读

    相关 内部匿名内部

    一.内部类 1.什么是内部类? 将一个类A定义在另一个类B里面,里面的那个类A就称为内部类,B则称为外部类 2.成员内部类 (1)成员内部类:定义在类中方法