内部类与Lambda表达式——常用API

发布于:2022-12-17 ⋅ 阅读:(152) ⋅ 点赞:(0)

内部类概念

  • 内部类定义格式

    • 格式&举例:

      /*
      	格式:
          class 外部类名{
          	修饰符 class 内部类名{
          	
          	}
          }
      */
      
      class Outer {
          public class Inner {
              
          }
      }

1.内部类的访问特点 

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

  • 外部类要访问内部类的成员,必须创建对象  

/*
    内部类访问特点:
        内部类可以直接访问外部类的成员,包括私有
        外部类要访问内部类的成员,必须创建对象
 */
public class Outer {
    private int num = 10;
    public class Inner {
        public void show() {
            System.out.println(num);
        }
    }
    public void method() {
        Inner i = new Inner();
        i.show();
    }
}

2.Lambda表达式的标准格式

  • 格式:

    (形式参数) -> {代码块}

    • 形式参数:如果有多个参数,参数之间用逗号隔开;如果没有参数,留空即可

    • ->:由英文中画线和大于符号组成,固定写法。代表指向动作

    • 代码块:是我们具体要做的事情,也就是以前我们写的方法体内容

  • 组成Lambda表达式的三要素:

    • 形式参数,箭头,代码块

 练习:

public interface Addable {
    int add(int x,int y);
}

public class AddableDemo {
    public static void main(String[] args) {
        //在主方法中调用useAddable方法
        useAddable((int x,int y) -> {
            return x + y;
        });

    }

    private static void useAddable(Addable a) {
        int sum = a.add(10, 20);
        System.out.println(sum);
    }
}

3.Lambda表达式的省略模式

省略的规则

  • 参数类型可以省略。但是有多个参数的情况下,不能只省略一个

  • 如果参数有且仅有一个,那么小括号可以省略

  • 如果代码块的语句只有一条,可以省略大括号和分号,和return关键字

4.Lambda表达式的使用前提

  • 使用Lambda必须要有接口

  • 并且要求接口中有且仅有一个抽象方法

 5.常用API

Math类

  • 1、Math类概述

    • Math 包含执行基本数字运算的方法

  • 2、Math中方法的调用方式

    • Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用

  • 3、Math类的常用方法

  • c static int abs(int a) 返回参数的绝对值
    public static double ceil(double a) 返回大于或等于参数的最小double值,等于一个整数
    public static double floor(double a) 返回小于或等于参数的最大double值,等于一个整数
    public static int round(float a) 按照四舍五入返回最接近参数的int
    public static int max(int a,int b) 返回两个int值中的较大值
    public static int min(int a,int b) 返回两个int值中的较小值
    public static double pow (double a,double b) 返回a的b次幂的值
    public static double random() 返回值为double的正值,[0.0,1.0)

System

System类的常用方法
  • public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
    public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
  • 示例代码:计算执行了多少毫秒
  • public class SystemDemo {
        public static void main(String[] args) {
            // 获取开始的时间节点
            long start = System.currentTimeMillis();
            for (int i = 1; i <= 10000; i++) {
                System.out.println(i);
            }
            // 获取代码运行结束后的时间节点
            long end = System.currentTimeMillis();
            System.out.println("共耗时:" + (end - start) + "毫秒");
        }
    }

Object类的toString方法

​​​
  • Object类概述

    • Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份

  • 查看方法源码的方式

    • 选中方法,按下Ctrl + B

  • 重写toString方法的方式

      1. Alt + Insert 选择toString

      1. 在类的空白区域,右键 -> Generate -> 选择toString

  • toString方法的作用:

    • 以良好的格式,更方便的展示对象中的属性值

    • class Student extends Object {
          private String name;
          private int age;
      
          public Student() {
          }
      
          public Student(String name, int age) {
              this.name = name;
              this.age = age;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public int getAge() {
              return age;
          }
      
          public void setAge(int age) {
              this.age = age;
          }
      
          @Override
          public String toString() {
              return "Student{" +
                      "name='" + name + '\'' +
                      ", age=" + age +
                      '}';
          }
      }
      public class ObjectDemo {
          public static void main(String[] args) {
              Student s = new Student();
              s.setName("林青霞");
              s.setAge(30);
              System.out.println(s); 
              System.out.println(s.toString()); 
          }
      }

      运行结果:

    • Student{name='林青霞', age=30}
      Student{name='林青霞', age=30}

Object类的equals方法(应用)

  • equals方法的作用

    • 用于对象之间的比较,返回true和false的结果

    • 举例:s1.equals(s2); s1和s2是两个对象

  • 重写equals方法的场景

    • 不希望比较对象的地址值,想要结合对象属性进行比较的时候。

  • 重写equals方法的方式

      1. alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可

      1. 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。

class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        //this -- s1
        //o -- s2
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o; //student -- s2

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }
}
public class ObjectDemo {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("林青霞");
        s1.setAge(30);

        Student s2 = new Student();
        s2.setName("林青霞");
        s2.setAge(30);

        //需求:比较两个对象的内容是否相同
        System.out.println(s1.equals(s2));
    }
}

面试题:
// 看程序,分析结果
String s = “abc”;
StringBuilder sb = new StringBuilder(“abc”);
s.equals(sb); 
sb.equals(s); 

public class InterviewTest {
    public static void main(String[] args) {
        String s1 = "abc";
        StringBuilder sb = new StringBuilder("abc");
        //1.此时调用的是String类中的equals方法.
        //保证参数也是字符串,否则不会比较属性值而直接返回false
        //System.out.println(s1.equals(sb)); // false

        //StringBuilder类中是没有重写equals方法,用的就是Object类中的.
        System.out.println(sb.equals(s1)); // false
    }
}

Objects (应用)

  • 常用方法

    方法名 说明
    public static String toString(对象) 返回参数中对象的字符串表示形式。
    public static String toString(对象, 默认字符串) 返回对象的字符串表示形式。
    public static Boolean isNull(对象) 判断对象是否为空
    public static Boolean nonNull(对象) 判断对象是否不为空
public class MyObjectsDemo {
          public static void main(String[] args) {
      //        public static String toString(对象): 返回参数中对象的字符串表示形式。
      //        Student s = new Student("小罗同学",50);
      //        String result = Objects.toString(s);
      //        System.out.println(result);
      //        System.out.println(s);

      //        public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.
              //Student s = new Student("小花同学",23);
      //        Student s = null;
      //        String result = Objects.toString(s, "随便写一个");
      //        System.out.println(result);
      
      //        public static Boolean isNull(对象): 判断对象是否为空
              //Student s = null;
      //        Student s = new Student();
      //        boolean result = Objects.isNull(s);
      //        System.out.println(result);

      //        public static Boolean nonNull(对象): 判断对象是否不为空
              //Student s = new Student();
              Student s = null;
              boolean result = Objects.nonNull(s);
              System.out.println(result);
          }
  }

BigDecimal 方法

方法名 说明
public BigDecimal add(另一个BigDecimal对象) 加法
public BigDecimal subtract (另一个BigDecimal对象) 减法
public BigDecimal multiply (另一个BigDecimal对象) 乘法
public BigDecimal divide (另一个BigDecimal对象) 除法
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) 除法

总结

  1. BigDecimal是用来进行精确计算的

  2. 创建BigDecimal的对象,构造方法使用参数类型为字符串的。

  3. 四则运算中的除法,如果除不尽请使用divide的三个参数的方法。

代码示例:

BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
参数1 ,表示参与运算的BigDecimal 对象。
参数2 ,表示小数点后面精确到多少位
参数3 ,舍入模式  
  BigDecimal.ROUND_UP  进一法
  BigDecimal.ROUND_FLOOR 去尾法
  BigDecimal.ROUND_HALF_UP 四舍五入

数组Arrays : 

方法名 说明
public static String toString(int[] a) 返回指定数组的内容的字符串表示形式
public static void sort(int[] a) 按照数字顺序排列指定的数组
public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引
public class MyArraysDemo {
      public static void main(String[] args) {
  //        public static String toString(int[] a)    返回指定数组的内容的字符串表示形式
  //        int [] arr = {3,2,4,6,7};
  //        System.out.println(Arrays.toString(arr));

  //        public static void sort(int[] a)	  按照数字顺序排列指定的数组
  //        int [] arr = {3,2,4,6,7};
  //        Arrays.sort(arr);
  //        System.out.println(Arrays.toString(arr));

  //        public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引
          int [] arr = {1,2,3,4,5,6,7,8,9,10};
          int index = Arrays.binarySearch(arr, 0);
          System.out.println(index);
          //1,数组必须有序
          //2.如果要查找的元素存在,那么返回的是这个元素实际的索引
          //3.如果要查找的元素不存在,那么返回的是 (-插入点-1)
              //插入点:如果这个元素在数组中,他应该在哪个索引上.
      }
  }

 


网站公告

今日签到

点亮在社区的每一天
去签到