zoukankan      html  css  js  c++  java
  • 构造方法引用

    构造方法引用


    构造方法引用与一般方法引用类似,只不过方法名为new。例如,Person::new是Persion构造方法的一个引用。若Person类存在多个构造方法该如何选择呢?着取决于上下文。

    可以用数组类型创建构造器引用。

    public class Main {
        public static void main(String[] args) {
            Integer[] integers = new Integer[]{1, 2, 3, 4, 5, 6};
            //1.
            Integer[] integers1 = Arrays.stream(integers).toArray(length -> new Integer[length]);
            //2.toArray方法调用这个构造器来得到一个正确类型的数组。然后填充这个数组并返回。
            Integer[] integers2 = Arrays.stream(integers).toArray(Integer[]::new);
        }
    }
    

    由于Java不能创建泛型数组,很多方法往往放回Object[],数组构造器引用对于克服这个限制很有用。


    《Java核心技术》阅读笔记

  • 相关阅读:
    根据坐标经纬度计算两点之间的距离
    C# 获取类名
    Post、Get请求
    Image和Base64相互转换
    Html checkbox全选
    .NET Core 中间件
    C# DataTable 用法
    imshow(A,[])和imshow(A)的区别
    Log-spectral distance
    CUDA
  • 原文地址:https://www.cnblogs.com/XiaoZhengYu/p/12861468.html
Copyright © 2011-2022 走看看