Java?Lambda?面向对象

读者指南

目的:只想简单地、清晰明了地介绍 方法、面向对象、lambda、函数式编程 这几个容易混淆的概念

参考资料:Lua(脚本语言)、Java(面向对象编程语言)、C++(面向过程和面向对象编程语言)

#### 方法

程序是代码和数据的结合,方法是可重用的代码片段。面向过程的编程中方法可以独立存在,访问全局变量;面向对象的编程中,方法配合对象使用,可以访问对象封装的数据。

#### Java面向对象编程

Java中方法无法独立存在,方法依赖于类和对象,一个方法要么是类的方法,要么是对象实例的方法。没有单独存在的函数,这也是Java和C++不同的地方。

#### lambda表达式

Java提供了一种语法糖,可以让函数以独立的形式存在,即Functional Interface(函数式接口,下面简称FI),所谓FI,就是用一个接口类型来表示某个函数,接口中只包含这一个函数,接口的使用方只能使用这一个函数,接口的提供方只需要提供这一个函数。所以lambda诞生了,lambda是函数的一种简写形式。lambda的特点也很明显了,它没有类和对象,没有this指针,只有函数闭包。lambda在形式上等同于独立的函数,lambda在Java中配合FI使用,只能通过FI接口类型来引用,本质上无法独立存在。Java依然是面向对象的。

#### 函数式编程

有一种纯粹的函数,它只从参数中获取信息,计算结果只通过返回值向外暴露,甚至不需要函数闭包。对它来言,相同的参数必然有相同的输出,而无论程序是串行或者并行。这类函数可以排列组合调用的顺序,组成更大的函数。它和lambda表达式又不一样。 
 
这里有一个Lua脚本使用函数的例子:
程序中有两个人小宇xiaoyu和小飞xiaofei,小宇做了三件事:应聘工作,向领导汇报工作,面试小飞;小飞做了一件事,面试时的自我介绍。Lua(脚本语言中)可以实现函数单独定义、调用时结合对象数据使用的功能(代码中xiaoyu.orderPersonToDo,换成object:function写法会更好理解)。这在Java中是不能实现的,Java中的lambda无法使用this指针。
xiaoyu = {
    ["name"]="xiaoyu",
    ["sex"]="male",
    ["age"]="26",
}

function xiaoyu.orderPersonToDo( person, action )
    local info = string.format("order %s to do", person.name)
    print(info)
    action(person)
end

function reportToBoss(self)
    local info = self.name .. string.format("( %s , %s years old ) is reporting to his boss", self.sex, self.age)
    print(info)
end

function applyForPosition(self)
    local info = self.name .. string.format("( %s , %s years old ) is applying for this position", self.sex, self.age)
    print(info)
end

function introduceSelfTo( self)
    local info = string.format("my name is %s, i'am %s years old", self.name, self.age)
    print(info)
end

xiaoyu.reportToBoss = reportToBoss
xiaoyu.applyForPosition = applyForPosition

xiaofei = {
    ["name"]="xiaofei",
    ["sex"]="male",
    ["age"]="24",
}

xiaoyu:applyForPosition()
xiaoyu:reportToBoss()

xiaoyu.orderPersonToDo(xiaofei, introduceSelfTo)
View Code
 

FI 肤浅的理解就是:函数注解的接口是这样一个类型,这个类型的作用相当于一个函数;这个接口定义切且只定义了一个抽象方法(实例方法),这个方法不可以和object方法重复。

单看这两个博客理解起来有些吃力,看看java1.8中对FounctionalInterface的解释:

/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang;

import java.lang.annotation.*;

/**
 * An informative annotation type used to indicate that an interface
 * type declaration is intended to be a <i>functional interface</i> as
 * defined by the Java Language Specification.
 * :functionalInterface注解用来指明一个接口类型是functional interface,这句话好像没什么用
 * Conceptually, a functional interface has exactly one abstract
 * method.  Since {@linkplain java.lang.reflect.Method#isDefault()
 * default methods} have an implementation, they are not abstract.  If
 * an interface declares an abstract method overriding one of the
 * public methods of {@code java.lang.Object}, that also does
 * <em>not</em> count toward the interface's abstract method count
 * since any implementation of the interface will have an
 * implementation from {@code java.lang.Object} or elsewhere.
 *:一个函数注解借口准确的说,只有一个抽象方法,且这个方法不能是重写的Object的public方法(不计入does not count toward)
 * <p>Note that instances of functional interfaces can be created with
 * lambda expressions, method references, or constructor references.
 *:一个函数注解借口可以使lambda表达式、方法的引用、或者构造函数的引用
 * <p>If a type is annotated with this annotation type, compilers are
 * required to generate an error message unless:
 *:使用这个注解的时候,为了使编译器不报错,至少要满足:
 * <ul>
 * <li> The type is an interface type and not an annotation type, enum, or class.
 * <li> The annotated type satisfies the requirements of a functional interface.
 * </ul>
 *:type是借口而不是注解、枚举、类
 * <p>However, the compiler will treat any interface meeting the
 * definition of a functional interface as a functional interface
 * regardless of whether or not a {@code FunctionalInterface}
 * annotation is present on the interface declaration.
 *:这里描述没用使用functionalInterface注解却被认作是函数注解接口的情况:meeting the definition of a functional interface即满足了函数接口的定义;
* 也就是说,FunctionalInterface注解其实可以省略不写,感觉好像被别人耍了一样,FunctionalInterface就是个概念 * @jls 4.3.2. The Class Object * @jls 9.8 Functional Interfaces * @jls 9.4.3 Interface Method Body * @since 1.8
*/ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FunctionalInterface {}

简单的例子:

Runnable接口早就有了,FunctionalInterface是1.8才出的,Runnable接口只有一个抽象函数,满足FounctionalInterface的定义,属于上面提到的特殊情况,在1.8中加上了@FunctionalInterface注解

Thread th=new Thread(()->System.out.println("hello"));
        th.start();

 有了上面的理解后,可以自己写一个Lambda的例子:运行结果是class java.lang.String

package com.lambda;

import java.util.concurrent.Callable;

public class HelloLambda {

    public void function(Callable callable){
        Object result=null;
        try {
// 这里使用的callable并没有单独开辟线程进行执行。开辟新线程执行需要使用Thead 的 start方法。 result
= callable.call(); System.out.println(result.getClass().toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { HelloLambda hello=new HelloLambda(); hello.function(()->new String("hello")); } }
原文地址:https://www.cnblogs.com/afraidToForget/p/6595970.html