• Java 双冒号 :: 的用法


    介绍

    双冒号(::)运算符在Java 8中被用作方法引用(method reference),方法引用是与lambda表达式相关的一个重要特性。它提供了一种不执行方法的方法。为此,方法引用需要由兼容的函数接口组成的目标类型上下文。

    用法

    静态方法引用(static method)语法:classname::methodname 例如:Person::getAge
    对象的实例方法引用语法:instancename::methodname 例如:System.out::println
    对象的超类方法引用语法: super::methodname
    类构造器引用语法: classname::new 例如:ArrayList::new
    数组构造器引用语法: typename[]::new 例如: String[]:new
    在这里插入图片描述
    1️⃣指向静态方法的引用
    2️⃣指向某个对象的实例方法的引用
    3️⃣指向某个类型的实例方法的引用
    4️⃣指向构造方法的引用

    实例

    对静态方法的引用

    类名 :: 方法名

    public class Test02 {
        public static void main(String[] args) {
            String[] array = {"aaaa", "bbbb", "cccc"};
            List<String> list = Arrays.asList(array);
            //引用静态方法,通过类名+::+方法名的方式
            list.forEach(demo::print);
        }
        public static void print(String s){
            System.out.println(s);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    示例二

    public class Demo {
    	@Test
    	public void test() {
    		List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");
    		
    		//静态方法语法	ClassName::methodName
    		list.forEach(Demo::print);
    	}
    	
    	public static void print(String content){
    		System.out.println(content);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    对对象的实例方法的使用

    对象名 :: 方法名

    public class Test03 {
        public static void main(String[] args) {
            String arr[]=new String[]{"aaa","bbb","ccc"};
            List<String> list = Arrays.asList(arr);
            Test03 test03=new Test03();
            //引用实例方法,通过对象+::+方法名的方式
            list.forEach(test03::print);
        }
        public void print(String s){
            System.out.println(s);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    示例二

    public class Demo {
    	@Test
    	public void test() {
    		List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");
    		//对象实例语法	instanceRef::methodName
    		list.forEach(new Demo()::print);
    	}
    	
    	public void print(String content){
    		System.out.println(content);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    对父类方法的引用

    public class Test04 extends Test04Fu {
        public  void test() {
            String arr[] =new String[]{"aaa","bbb","ccc"};
            List<String> list = Arrays.asList(arr);
            //引用父类方法,通过super+::+方法名的方式
            list.forEach(super::print);
        }
    }
    class Test04Fu{
        void print(String s){
            System.out.println(s);
        }
    }
    class Test05{
        public static void main(String[] args) {
            Test04 test04=new Test04();
            test04.test();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    类实例方法的使用

    public class Demo {
    	@Test
    	public void test() {
    		List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");
    		
    		//对象实例语法	instanceRef::methodName
    		list.forEach(new Demo()::print);
    	}
    	
    	public void print(String content){
    		System.out.println(content);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    引用构造器

    无参构造器

    public class Test06 {
        public void test() {
        //引用构造器使用类名::new 
            InterfaceExample com =  Test06::new;
            Test06 bean = com.create();
            System.out.println(bean);
        }
    }
    
    interface InterfaceExample{
        Test06 create();
    }
    class Test07{
        public static void main(String[] args) {
            Test06 test06=new Test06();
            test06.test();
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    有参构造器

    public class Test06 {
        public void test() {
        //引用构造器使用类名::new 
            InterfaceExample com =  Test06::new;
            Test06 bean = com.create();
            System.out.println(bean);
        }
    }
    
    interface InterfaceExample{
        Test06 create();
    }
    class Test07{
        public static void main(String[] args) {
            Test06 test06=new Test06();
            test06.test();
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    特别注意的是:Example 类并没有implements InterfaceExample接口

    数组构造器

    借用jdk自带的java.util.function.Function类实现
    public class Example {
    	public static void main(String[] args) {
    		Function <Integer, Example[]> function = Example[]::new;
    		Example[] array = function.apply(4);	//这里的4是数组的大小
    		
    		for(Example e:array){
    			System.out.println(e);	//如果输出的话,你会发现会输出4个空对象(null)
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    自定义接口
    public class Example {
    	
    	public static void main(String[] args) {
    		Interface <Integer, Example[]> function = Example[]::new;
    		Example[] array = function.apply(4);	//这里的4是数组的大小
    		
    		for(Example e:array){
    			System.out.println(e);
    		}
    	}
    }
    
    @FunctionalInterface
    interface Interface<A, T>{
    	T apply(A a); 
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    Java开发扫雷游戏项目,JFram类的使用
    “随机森林”及“混合随机森林和多目标粒子群优化”(RF_MOPSO),以预测目标作为学习方法并分别找到多特征过程的最佳参数(Matlab代码实现)
    live555
    Environment Modules工具
    裁员浪潮,回顾一下自己去年的毕业吧(二)
    网络安全(黑客)自学笔记
    【PC】2023年11月商店更新
    ZYNQ图像处理项目——模板匹配数字识别(3)
    Qemu镜像安全加密测试
    通知定档2024中国(成都)国际线路板展览会
  • 原文地址:https://blog.csdn.net/qq_41681845/article/details/127105469