/**
* 定义方法,参数中含有Function
* @param valueToBeOperate
* @param function
*/
public void modifyTheValue(int valueToBeOperate,Function function){
int newValue = function.apply(valueToBeOperate);//根据输入的参数,然后计算出结果
System.out.println(newValue);
}
@Test
public void testFunction(){
int incr = 20;
int myNumber = 10;
modifyTheValue(myNumber,var -> var + incr + 100);//var 相当于myNumber
}
FunctionalInterface关键字是放在接口中的注解,用于标示此接口可用于函数式编程形式 如”() ->{}“ 的写法,形式如下
@FunctionalInterface
public class Flyer{
String fly(String name);
}
@FunctionalInterface只能用在中只有一个虚方法的接口中,但接口中可以有其他有某认实现的方法或者静态方法
如下所示
@FunctionalInterface
public interface Flyer {
String fly(String name);
default int flyMaxHight(){
return 1000;
}
static void flyMaxDistance(){
System.out.println("最远里程");
}
}
但是@FunctionalInterface不能放在有一个以上虚方法的接口中,会报编译错误,如下所示
@FunctionalInterface
public interface Flyer {//报编译错误
String fly(String name);
void getSpeed();
default int flyMaxHight(){
return 1000;
}
static void flyMaxDistance(){
System.out.println("最远里程");
}
}
定义
/**
* 飞行器
* @author 734621
*
*/
@FunctionalInterface
public interface Flyer {
String fly(String name);
default int flyMaxHight(){
return 1000;
}
static void flyMaxDistance(){
System.out.println("最远里程");
}
}
测试
public class TestFlyer {
@Test
public void testPlane(){
//调用plane方法
plane((name)->{//函数式编程
return "我是"+name;
});
// 或者
plane(name-> "我是"+name );
}
private void plane(Flyer flyer) {
System.out.println(flyer.fly("飞机"));
}
}
package com.sf.simba.function.consumer;
/**
* 衣服类
* @author 734621
*
*/
public class Clothes {
private Double price;//衣服价格
private String name;//衣服名称
public Clothes(String name,Double price){
this.name = name;
this.price = price;
System.out.println(name + "\r\n原价:" + price + "元");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
package com.sf.simba.function.consumer;
/**
* 客户类
* @author 734621
*
*/
public class Customer {
private String name;
private int level;//用户等级VIP
private Double freaCount = 0.0;//折扣
private Clothes clothes;
public Customer(String name,int level,Clothes clothes){
this.name = name;
this.level = level;
this.clothes = clothes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public Double getFreaCount() {
return freaCount;
}
public void setFreaCount(Double freaCount) {
this.freaCount = freaCount;
}
public Clothes getClothes() {
return clothes;
}
public void setClothes(Clothes clothes) {
this.clothes = clothes;
}
/**
* 计算折扣后的价格
* 折扣后的价格=衣服原价 - 客户折扣*衣服原价
* 如折扣率为30表示可以折扣30%,即折扣后的价格为: 衣服原价*70%
* @return
*/
public Double getTruePrice(){
Double result = 0D;
result = clothes.getPrice() - ((getFreaCount()*clothes.getPrice())/100);
System.out.println(getName() + ",根据你的等级,"+clothes.getName()+"打折后的价格是:" + result + "元");
return result;
}
}
package com.sf.simba.function.consumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.junit.Test;
/**
* 买衣服的服务类
* @author 734621
*
*/
public class TransactionService {
/**
* 客户买衣服
* @param customer
* @param predicate
* @param consumer
* @return
*/
public Customer buyClothes(Customer customer,Predicate predicate,Consumer consumer){
if(predicate.test(customer)){//判断客户的等级是否达到折扣的条件 如果达到了就可以折扣
consumer.accept(customer);//打折扣
}
return customer;
}
@Test
public void testBuyClothes(){
Clothes clothes = new Clothes("羽绒服",1000D);//定义羽绒服,原价为1000
Customer customers1 = new Customer("Simba",6,clothes);
customers1 = buyClothes(customers1,
customer -> customer.getLevel()>=5,//等级大于等于5才有折扣
customer -> customer.setFreaCount(20D));//折扣是20%
customers1.getTruePrice();//获得最好的价格
}
}