zoukankan      html  css  js  c++  java
  • JDK8新特性,方法的引用

    引用方法并运行

    在Java中,方法和构造方法都看作是对象的一种,那么你要引用它(不是调用),则可以用::来引用。用来存储这个引用的类型用@FunctionlaInterface注解来标识。

    示例:

    package fun;
    
    /**
     * @author 施俊杰
     * @email shijunjie.me@outlook.com
     */
    public class TestMethods {
        
        @FunctionalInterface
         interface Fun<F1, F2, T> {
             T myrun(F1 from1, F2 from2);
         }
        
         public int add(int x, int y) {
             return x+y;
         }
        
        public static void main(String[] args) {
            Fun<Integer, Integer, Integer> f = new TestMethods()::add;
            int i = f.myrun(1, 2);
            System.out.println(i);
            System.out.println(f.toString());
        }
        
    }

    运行结果:

    通过引用构造函数来创建对象

    示例如下

    package interfacetest;
    
    /**
     * @author 施俊杰
     * @email shijunjie.me@outlook.com
     */
    public class TestMethods2 {
        private int i;
        private int j;
        public TestMethods2(int i, int j) {
            this.i = i;
            this.j = j;
        }
        public int getI() {
            return i;
        }
        public void setI(int i) {
            this.i = i;
        }
        public int getJ() {
            return j;
        }
        public void setJ(int j) {
            this.j = j;
        }
        
        @Override
        public String toString() {
            return "TestMethods2 [i=" + i + ", j=" + j + "]";
        }
    
    
    
        @FunctionalInterface
        interface constructor<F1, F2, T> {
            T create(F1 f1, F2 f2); 
        }
    
        public static void main(String[] args) {
            constructor<Integer, Integer, TestMethods2> c = TestMethods2::new;
            TestMethods2 obj = c.create(1, 2);
            System.out.println(obj);
        }
    }

    运行结果:

  • 相关阅读:
    Buffer cache spillover: only buffers
    11g中如何禁用自动统计信息收集作业
    OTN中文技术论坛清净的ORACLE讨论之地
    关闭磁盘自动运行
    #error
    在VC++中实现无标题栏对话框的拖动
    string,CString,char*之间的转化
    关于注册表
    #pragma once
    extern "C"用法详解
  • 原文地址:https://www.cnblogs.com/s648667069/p/8865395.html
Copyright © 2011-2022 走看看