术语,它允许创建流畅的API?我想答案是“这取决于”;-)对于像plus(一个被重写的操作符)这样的事情,我想说,对于链结的事情,答案几乎是肯定的。我可以想出一些人为的例子,但事实并非如此。使用Groovy,您当然可以向Integer添加一个plus(Mo

术语,它允许创建流畅的API?我想答案是“这取决于”;-)对于像plus(一个被重写的操作符)这样的事情,我想说,对于链结的事情,答案几乎是肯定的。我可以想出一些人为的例子,但事实并非如此。使用Groovy,您当然可以向Integer添加一个plus(Mo,groovy,Groovy,术语,它允许创建流畅的API?我想答案是“这取决于”;-)对于像plus(一个被重写的操作符)这样的事情,我想说,对于链结的事情,答案几乎是肯定的。我可以想出一些人为的例子,但事实并非如此。使用Groovy,您当然可以向Integer添加一个plus(Money)方法,但这会破坏;-) class Money{ private Integer amount Money(Integer amt){ amount = amt } /* *

术语,它允许创建流畅的API?我想答案是“这取决于”;-)对于像
plus
(一个被重写的操作符)这样的事情,我想说,对于链结的事情,答案几乎是肯定的。我可以想出一些人为的例子,但事实并非如此。使用Groovy,您当然可以向Integer添加一个
plus(Money)
方法,但这会破坏;-)
class Money{
    private Integer amount

    Money(Integer amt){
        amount = amt
    }

    /*
     * Shows how to overload the + operator
     */

    Integer plus(Money more){
        int total
        total = this.amount + more.amount
        return total
    }

    Integer plus(Integer more){
        int total
        total = this.amount + more
        return total
    }

    Integer plus(BigDecimal more){ // multiple dispatch
        int total = more.plus(this.amount)
        return total
    }

    @Override
    String toString(){
        return "$amount"
    }
}

def buck = new Money(1000)
println buck + 1000.0G  
def firstMoney = new Money(15)
def secondMoney = firstMoney + 10