最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。
最近的书本学习已经接近尾声了;后面的章节还有一些零散的知识点;今天就来说一说Java8的一个新特性——Lambda表达式。
1,Lambda表达式简介
Lambda表达式本质是一个匿名方法,没有方法名,没有修饰符,也没有返回值类型;使用其编码更加简洁。使用该特性首先需要配置/app/build.gradle文件。需要在android{}中添加如下代码:
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8;
targetCompatibility JavaVersion.VERSION_1_8;
}
使用条件:
当一个只含有一个方法的接口作为其他方法的参数时,我们在传参时对该接口中唯一方法实现时。比如,通过Runnable接口创建一个线程时;实现界面点击事件时(可以跟源码,看看Runnable接口和View.onClickLister接口中是否都只有一个方法)。
2,示例代码
我这里不用系统提供的接口,自定义三个接口,里面的三个方法参数个数分别为0,1,2个;下面是具体代码:
TestIWithoutParam.java代码:
package com.hfut.operationlambda; /** * author:why * created on: 2018/3/27 15:35 * description: */ public interface TestIWithoutParam { public void testMethod(); }
TestIWithParam.java代码:
package com.hfut.operationlambda; /** * author:why * created on: 2018/3/27 15:33 * description: */ public interface TestIWithParam { public void testMethod(String name); }
TestIWithParams.java代码:
package com.hfut.operationlambda; /** * author:why * created on: 2018/3/27 17:04 * description: */ public interface TestWithParams { public void testMethod(String name,int age); }
MainActivity.java代码:
package com.hfut.operationlambda; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void exeTestIWithoutParam(View view){ showTestIWithoutParam(()->{ Log.i(TAG, "exeTestIWithoutParam: "); }); } public void exeTestIWithParam(View view){ showTestIWithParam((String name)->{ Log.i(TAG, "exeTestIWithParam:"+name); }); // showTestIWithParam(v->{ // Log.i(TAG, "exeTestIWithoutParam: "); // }); } public void exeTestIWithParams(View view){ showTestIWithParams((String name,int age)->{ Log.i(TAG, "exeTestIWithParams: "+name+" "+age); }); } private void showTestIWithoutParam(TestIWithoutParam testIWithoutParam){ testIWithoutParam.testMethod(); } private void showTestIWithParam(TestIWithParam testIWithParam){ testIWithParam.testMethod("why"); } private void showTestIWithParams(TestWithParams testIWithParams){ testIWithParams.testMethod("why",26); } }
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.hfut.operationlambda.MainActivity"> <Button android:layout_marginTop="30dp" android:textSize="20dp" android:text="Lambda表达式测试函数无参" android:onClick="exeTestIWithoutParam" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_marginTop="10dp" android:textSize="20dp" android:text="Lambda表达式测试函数有单参" android:onClick="exeTestIWithParam" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_marginTop="10dp" android:textSize="20dp" android:text="Lambda表达式测试函数有多参" android:onClick="exeTestIWithParams" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
3,运行结果
第一步:运行程序
第二步:依次点击“LAMBDA表达式测试函数无参”,点击“LAMBDA表达式测试函数有单参”按钮和点击“LAMBDA表达式测试函数有单参”按钮
总结:该特性对于刚使用的人还是蛮不习惯的,我也有这样的感觉,但是不得不说,确实代码要简洁许多;可以结合个人意愿;还有就是在函数是单个参数的时候,有两种方式实现;我在代码中注释了另外一种。