java中的函数式编程(一)

        当你安安稳稳的学着java,慢慢开始写代码。

        兢兢业业,本着面向对象的编程方式。

        知道有一种叫做“面向过程”的方式,但是你不在意。

        写了一段时间后有人告你,函数式编程很爽!

        你也看到了他给的一个实例,看着不错。

        于是你心里有了一个疑问:特么的什么是“函数式编程”?

 

        在说“函数式编程”之前,必须说明白一件事:"表达式(expression)"和"语句(statement)"

        最简单的区分是“表达式”指的是有返回值的方法;“语句”是无返回值的方法;

        从进化的底层来讲,我们总共处理了两件事:运算和记录。“表达式”和“语句”就与此相对应。

        "表达式"(expression)是一个单纯的运算过程,总是有(因为需要)返回值;函数式编程中我们使用互相使用返回值,所以只使用表达式,不使用语句。也就是说,每一步都是单纯的运算,而且都有返回值。

        "语句"(statement)是执行某种操作,没有返回值(或者说不需要考虑,忽略返回值)。

 

        函数式编程的开发动机:一开始就是为了处理运算(computation),不设计处理记录。"语句"属于记录信息,所以就被排斥在外。当然,实际应用中,光运算不记录是没有意义的,运算就是为结果。因此,编程过程中,函数式编程只要求把记录简化到最小,不要有不必要的读写行为,保持计算过程的单纯性。

 

        看到这里,我相信你似乎了解了什么。

        没错,函数式就是TM的有返回的相互调用。

        回头再看看自己的代码,哎呦

        好像也有那么一两个函数式,只是没连起来。

        对于开发而言,最初的便利只是减少代码量。

        那么好吧,是时候举个栗子了。

 

        先封装一个运算的类Operations.class:

package test;

/**
 * 对运算做一个封装,包括“+,-,*,/”四则
 * @author ShuaiZhang
 *
 */
public class Operations {

	private double x;
	private Operations(double x){
		this.x = x;
	}
	//初始化包装对象
	public static Operations create(double initValus){
		return new Operations(initValus);
	}
	
	public Operations add(double add){
		return new Operations(x + add);
	}
	
	public Operations subtraction(double sub){
		return new Operations(x - sub);
	}
	
	public Operations multiply(double mul){
		return new Operations(x * mul);
	}
	
	public Operations division(double mul){
		return new Operations(x / mul);
	}
	//“记录”方法,返回实际值
	public double toDouble(){
		return x;
	}
	
/*=============两个参数来运算,用结果直接构建对象=====================================*/	
	
	public static Operations add(double first,double second){
		return new Operations(first + second);
	}
	
	public static Operations subtraction(double first,double second){
		return new Operations(first - second);
	}
	
	public static Operations multiply(double first,double second){
		return new Operations(first * second);
	}
	
	public static Operations devision(double first,double second){
		return new Operations(first / second);
	}
}




 

        接下来是测试类Test.class:


package test;

public class Test {
	
	public static double add(double first,double second){
		return first + second;
	}
	
	public static double subtraction(double first,double second){
		return first - second;
	}
	
	public static double multiply(double first,double second){
		return first * second;
	}
	
	public static double division(double first,double second){
		return first / second;
	}
	public static void main(String[] args) {
		//计算(1+2)/3*4
		
		//最基础的分布运算
		double first = 1+2;
		double second = first/3;
		double result1 = second*4;
		System.out.println("retult1:" + result1);
		
		//函数式实现计算
		double result2 = multiply(division(add(1,2),3),4);
		System.out.println("retult2:" + result2);
		
		//函数式形变:链式
//		double result3 = Operations.create(1).add(2).division(3).multiply(4).toDouble();
		double result3 = Operations.add(1,2).division(3).multiply(4).toDouble();
		System.out.println("retult3:" + result3);

		
	}
}

        

        Test.class的输出结果如下:


        我们再来个操作对象的例子:

        新建类UserInfo.class:

package test;

public class PersonInfo {

	private String firstName;
	private String secondName;
	private int age;
	private String gender;
	public String getFirstName() {
		return firstName;
	}
	public String getSecondName() {
		return secondName;
	}
	public int getAge() {
		return age;
	}
	public String getGender() {
		return gender;
	}
	//由于程序不允许同参同名方法,这些被注释掉了
//	public void setFirstName(String firstName) {
//		this.firstName = firstName;
//	}
//	public void setSecondName(String secondName) {
//		this.secondName = secondName;
//	}
//	public void setAge(int age) {
//		this.age = age;
//	}
//	public void setGender(String gender) {
//		this.gender = gender;
//	}


	//这里比传统的setter方法多了返回类型,并且返回了当前类对象自己
	public PersonInfo setFirstName(String firstName) {
		this.firstName = firstName;
		return this;
	}
	public PersonInfo setSecondName(String secondName) {
		this.secondName = secondName;
		return this;
	}
	public PersonInfo setAge(int age) {
		this.age = age;
		return this;
	}
	public PersonInfo setGender(String gender) {
		this.gender = gender;
		return this;
	}
	
	public String toString(){
		return "姓名:"+this.firstName+"-"+this.secondName+"\n性别:"+this.gender + "\n年龄:" + this.age;
	}
	
}

        在Test.class 中main方法新加如下代码:

		System.out.println("===============大分割线===========");
		System.out.println("===============分大割线===========");
		System.out.println("===============分割大线===========");
		System.out.println("===============分割线大===========");
		

		PersonInfo info1 = new PersonInfo();
		info1.setFirstName("Tom");
		info1.setSecondName("Jerry");
		info1.setAge(18);
		info1.setGender("man");
		System.out.println("人物信息:\n"+info1.toString());
		PersonInfo info2 = new PersonInfo();
		
		System.out.println("===============分割线===========");
		
		info2.setFirstName("汤姆").setSecondName("杰瑞").setGender("男").setAge(18);
		System.out.println("人物信息:\n"+info2.toString());
		

        整体的运行结果:



        从结果上来看,两个实体的内容是一样的,但是从代码量上来讲,这里大概只减少约三分之一,如果我们这里包含多个不同类型变量的初始化,那么代码量的差距将更大,理论上的差距大约是二十比一。就算没有那么夸张,极大的提高代码书写速度是肯定的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

公贵买其鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值