• java基础-四种方法引用


    实例

      直接三角形,通过两边算第三边,目的是为了如何使用这几种方法引用。代码中多有些不合适,尽情原谅。

    静态方法引用

       接口的参数列表与类中的具体实现方法的参数列表一样,返回值一致。

       调用

        

        //静态引用
        IStaticSide staticSide = Five::getStaticSize;
        staticSide.getStaticSize(3, 6);

        

       类中定义

      

        //静态引用
        public static double getStaticSize(double s,double s2)
        {
          return Math.sqrt(s*s+s2*s2);
        }

      定义接口


        interface IStaticSide{

          double getStaticSize(double s,double s2);
        }

    实例引用

      接口的参数列表与类中的具体实现方法的参数列表一样,返回值一致。

      调用

      

      Five five = new Five(3,4);

      //实例引用方法
      ILastSide side=five::getTheLastSide;
      System.out.println("实例引用,方法第三边的结果:"+side.getLastSide());

      类中定义

      // 类型引用
      public double getFiveLastSide(Five f) {
        return Math.sqrt(this.side1 * this.side1 + this.side1 * this.side1);
      }

      定义接口

      interface ILastSide {
        double getLastSide();
      }

    类型引用

        类中定义的方法只有一个参数,就是对比的类,也就是定义接口中的第二个参数
               定义的接口必须是两个参数,第一个参数指的引用的类,第二个参数指的是对比的类
      调用

      //类型引用
      ILastFiveSide typeside = Five::getFiveLastSide;
      System.out.println("类型引用,第三边的结果:"+typeside.getLastSide(five, five));

      类中定义

      // 类型引用
      public double getFiveLastSide(Five f) {
        return Math.sqrt(this.side1 * this.side1 + this.side1 * this.side1);
      }

      接口定义

      interface ILastFiveSide {

        double getLastSide(Five f, Five f2);

      }

    构造引用

      引用构造器,通过例子也就是引用后返回的是对象,然后通过对象再调用相应的方法执行。

      调用

      //构造器引用
      IGetInstance instace = Five::new;
      System.out.println("构造器引用,第三边的结果:"+instace.getInstance(1,2).getTheLastSide());

      类中定义

      // 构造器引用
      public Five getInstance(double s, double s2) {
        return new Five(s, s2);
      }

      接口定义

      // 接收引用后的结果接口
      interface IGetInstance {

        Five getInstance(double s, double s2);
      }

    后话

      只是自己通过简单的例子总结,如有不对的地方希望大神门指点江山。自己之前都是从事.net开发,java方面的知识确实不足,希望多多谅解,不想坑队友。

  • 相关阅读:
    django wsgi nginx 配置
    supervisor error: <class 'socket.error'>, [Errno 110]
    gunicorn 启动无日志
    获取windows 网卡GUID和ip信息
    亚马逊EC2根硬盘空间扩容
    pypcap 安装
    mysql 1709: Index column size too large. The maximum column size is 767 bytes.
    mysql死锁检查
    D3.js画思维导图(转)
    用D3.js画树状图
  • 原文地址:https://www.cnblogs.com/hackerxiaoyon/p/6964782.html
Copyright © 2020-2023  润新知