级联和链接之间的差异是什么?

时间:2021-11-27 00:20:15

I just find from a forum about cascade. The question was what does cascading means in poo. I tried to find to google the answer, also tried to find some other * threads about if but I couldn't. I just find this link http://en.wikipedia.org/wiki/Method_cascading

我只是从一个关于级联的论坛中找到。问题是级联在大便中意味着什么。我试图找到谷歌的答案,也试图找到一些其他*线程,但我不能。我只是找到这个链接http://en.wikipedia.org/wiki/Method_cascading

I know what is chaining, I used it, most in javascript, jquery and other languages, but I coundn't understand the difference between chaining and cascading. Can anybody help me? Or can anybody provide some useful links regarding to this?

我知道什么是链接,我使用它,大多数是在javascript,jquery和其他语言中,但我不理解链接和级联之间的区别。有谁能够帮助我?或者任何人都可以提供一些有用的链接吗?

2 个解决方案

#1


4  

Chaining is where you return the result of the method call to be used in the next call.

链接是返回方法调用结果的地方,用于下次调用。

c#

C#

Enumerable.Range(0,10).Skip(1).Aggregate(myList.First(),(result,listItem) => result += listItem));
//results in 45 being returned

Cascading can be implemented by using chaining when this is returned (making it sometimes tricky to differentiate between the two). jQuery does this.

当返回时,可以通过使用链接来实现级联(使得有时难以区分这两者)。 jQuery做到了这一点。

jquery

jQuery的

$("#myId").css("background-color","blue").fadeIn().fadeOut();
//results in $("#myId") being returned

#2


1  

The definition is pretty clear on the Wikipedia page you linked:

您链接的*页面上的定义非常清楚:

Given a method call a.b(), after executing the call, method cascading evaluates this expression to the left object a (with its new value, if mutated), while method chaining evaluates this expression to the right object.

给定一个方法调用a.b(),在执行调用之后,方法级联将此表达式计算为左对象a(具有其新值,如果已突变),而方法链接将此表达式计算为正确的对象。

This means that, a.b() returns a mutated a instance with method cascading. a.b() returns something different from b() with method chaining.

这意味着,a.b()返回带有方法级联的变异实例。 a.b()通过方法链返回与b()不同的东西。

So, this is method cascading:

所以,这是方法级联:

class YourClass {
    public YourClass b() {
        // do stuff
        return this;
    }

    public YourClass c() {
        // do stuff
        return this;
    }
}

..which allows: yourClass.b().c();.

..which允许:yourClass.b()。c();.

..and this is method chaining:

..这是方法链接:

class YourClass {
    public SomeOtherObject b() {
        // do stuff
        return new SomeOtherObject(this);
    }
}

class SomeOtherObject {
    private YourClass _owner;

    public SomeOtherObject(YourClass owner) {
        _owner = owner;
    }

    public void c_onOtherObject() {
    }
}

..which allows: yourClass.b().c_onOtherObject();.

..which允许:yourClass.b()。c_onOtherObject();.

EDIT: I rolled back my previous edit. It appears the above is correct and the terms aren't flipped incorrectly.

编辑:我回滚了我以前的编辑。看来上面的内容是正确的,并且术语不会错误地翻转。

#1


4  

Chaining is where you return the result of the method call to be used in the next call.

链接是返回方法调用结果的地方,用于下次调用。

c#

C#

Enumerable.Range(0,10).Skip(1).Aggregate(myList.First(),(result,listItem) => result += listItem));
//results in 45 being returned

Cascading can be implemented by using chaining when this is returned (making it sometimes tricky to differentiate between the two). jQuery does this.

当返回时,可以通过使用链接来实现级联(使得有时难以区分这两者)。 jQuery做到了这一点。

jquery

jQuery的

$("#myId").css("background-color","blue").fadeIn().fadeOut();
//results in $("#myId") being returned

#2


1  

The definition is pretty clear on the Wikipedia page you linked:

您链接的*页面上的定义非常清楚:

Given a method call a.b(), after executing the call, method cascading evaluates this expression to the left object a (with its new value, if mutated), while method chaining evaluates this expression to the right object.

给定一个方法调用a.b(),在执行调用之后,方法级联将此表达式计算为左对象a(具有其新值,如果已突变),而方法链接将此表达式计算为正确的对象。

This means that, a.b() returns a mutated a instance with method cascading. a.b() returns something different from b() with method chaining.

这意味着,a.b()返回带有方法级联的变异实例。 a.b()通过方法链返回与b()不同的东西。

So, this is method cascading:

所以,这是方法级联:

class YourClass {
    public YourClass b() {
        // do stuff
        return this;
    }

    public YourClass c() {
        // do stuff
        return this;
    }
}

..which allows: yourClass.b().c();.

..which允许:yourClass.b()。c();.

..and this is method chaining:

..这是方法链接:

class YourClass {
    public SomeOtherObject b() {
        // do stuff
        return new SomeOtherObject(this);
    }
}

class SomeOtherObject {
    private YourClass _owner;

    public SomeOtherObject(YourClass owner) {
        _owner = owner;
    }

    public void c_onOtherObject() {
    }
}

..which allows: yourClass.b().c_onOtherObject();.

..which允许:yourClass.b()。c_onOtherObject();.

EDIT: I rolled back my previous edit. It appears the above is correct and the terms aren't flipped incorrectly.

编辑:我回滚了我以前的编辑。看来上面的内容是正确的,并且术语不会错误地翻转。