我是靠谱客的博主 快乐汉堡,这篇文章主要介绍包装类和内部类--简单讲解,现在分享给大家,希望可以做个参考。

    • 包装类
      • 包装类的转换
      • 包装类的层次结构
      • 包装类的转换
      • 自动装箱和自动拆箱
    • 内部类
      • 成员内部类
        • 成员内部类代码
      • 内部类的作用
      • 静态内部类
        • 静态内部类代码
      • 局部内部类
        • 局部内部类代码
      • 匿名内部类
        • 匿名内部类代码一
        • 匿名内部类代码二

包装类

这里写图片描述

包装类的转换

这里写图片描述

包装类的层次结构

  • 除了Boolean和Character外,其他的包装类都有valueOf()和parseXXX()方法,并且还具有 byteValue()、shortValue()、intValue()、longValue()、floatValue()、doubleValue()方法,这些方法时最常见的方法。
    这里写图片描述

包装类的转换

复制代码
1
2
3
4
5
6
7
8
9
10
11
int转换为Integer int i=10; Integer I=new Integer(i); //这样更好,会利用缓存 Integer I = Integer.valueOf(i); Integer转换为int Integer i=new Integer(10); int i=I.intValue();

复制代码
1
2
3
4
5
6
7
8
9
String转换为Integer String s="12"; Integer I=new Integer(s); Integer I = Integer.valueOf(s); //更好 Integer转换为String Integer I=new Integer(23); String s=I.toString();

复制代码
1
2
3
4
5
6
7
8
9
String转换为int String s="20"; int i=Integer.parseInt(s); int转换为String int i=20; String s1=String.valueOf(i); String s2=""+i;

自动装箱和自动拆箱

  • java5.0开始支持自动装箱和自动拆箱,包装类的变量可以被赋值为一个基本类型—装箱;基本类型的变量可以被赋值为一个包装类

    复制代码
    1
    2
    3
    4
    5
    //自动装箱 Integer itg = 123; //自动拆箱 Int i = Integer.valueOf(111);
  • 注意:

    • 程序中少量的自动拆箱和装箱可以,但是大量的拆箱和装箱会对效率影响较大,所以尽量需要什么类型就声明什么类型。
      Integer类的自动转换

      复制代码
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      /** *实际上,程序运行时,系统为我们执行了下面一条语句 * Integer number = new Integer(290); */ Integer number = 290; System.out.println(number); /* * 实际上,程序运行时,系统为我们执行了下面语句 * int number2 = number.intValue(); */ Integer number = 290; int number2 = number; System.out.println(number2);

内部类

  • 在类的内部定义一个类
  • 内部类是一种编译时语法
  • 内部类分为四种
    • 成员内部类
    • 静态内部类
    • 局部内部类
    • 匿名内部类

成员内部类

  • 内部类的作用
    • 内部类可以访问外部类的私有成员
  • 类似于成员变量,可以使用访问修饰符
  • 构造一个成员内部类对象必须先够近啊一个外部类对象,然后通过外部类对象的引用“.new”构造内部类对象。

    Outer out1 = new Outer(200);
    Outer.Inner in = out1.new Inner();

  • 在内部类中访问外部类的成员变量,并不会破坏封装

    复制代码
    1
    2
    Outer.this.index
  • 成员内部类中不能有静态的变量

成员内部类代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Outer { private int index; public Outer(int index) { this.index = index; } class Inner { private int index = 100; public void print() { System.out.println(this.index); System.out.println(Outer.this.index); } } } // 测试的main方法 public static void main(String args[]) { Outer out1 = new Outer(200); Outer.Inner in = out1.new Inner(); in.print(); }

内部类的作用

  • 配合接口,抽象类实现多继承,这是使用内部类的主要原因

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public abstract class Person { public abstract void run(); } public interface Machine { void run(); } public class Robot extends Person { class RobotHeart implements Machine { public void run() { System.out.println("机器人发动机开动"); } } public void run() { System.out.println("人在跑"); } }

静态内部类

  • 用static修饰的内部类称为静态内部类
  • 在静态内部类中可以有静态的成员
  • 静态内部类只能访问外部类的静态成员,但是不能访问外部类的非静态成员
  • 构造静态内部类对象时不需要外部类对象

    复制代码
    1
    2
    Outer.Inner in = new Outer,Inner();

静态内部类代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Outer { private int index; private static int a = 200; static class Inner { private static int b = 300; public void print() { System.out.println(a + " " + b); } } } // 测试的main方法 public static void main(String args[]){ Outer.Inner in = new Outer.Inner(); in.print(); }

局部内部类

  • 定义在外部类方法中的内部类称为局部内部类,不能使用访问修饰符,但是可以使用abstract和final修饰符
  • 局部内部类的范围是在该方法内部
  • 可以访问外部类的属性,还可以访问外部类的有效的局部变量,但是要求这个局部变量必须是final的
  • 内部类的作用
    • 接口公开,却把接口的实现类作为内部类隐藏起来,强制用户通过接口来访问接口的实现类,强制达到弱耦合性

局部内部类代码

这里写图片描述

匿名内部类

  • 一个匿名内部类是一个特殊局部内部类
  • 这个内部类一定是用来继承一个类或者实现一个接口的,而且我们只会创建这个内部类的一个对象
  • 可以出现在方法的返回值类型中,也可以出现在方法的参数中
  • 不能定义构造方法

匿名内部类代码(一)

这里写图片描述

匿名内部类代码(二)

这里写图片描述

最后

以上就是快乐汉堡最近收集整理的关于包装类和内部类--简单讲解的全部内容,更多相关包装类和内部类--简单讲解内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部