Groovy overload operator

Keywords: Java Linux Selenium Programming

Heavy load for a while, heavy load for a long time.

Recently, I read a book "Groovy in action" to review some of Groovy's syntax features. I am infatuated with the function of this overloaded operator, and I insist on the cool and don't want. Share a Demo.

Because Groovy syntax is slightly different from Java syntax, but it is basically fully compatible with Java syntax, this Demo is still written in Java syntax, which is convenient for everyone to understand.

package com.FunTester.demo

import com.fun.frame.SourceCode

class demo1 extends SourceCode {

    public static void main(String[] args) {
       def s = "fun" * 3 << "fan"

        println s

        Demo demo = new Demo()
        Demo a = new Demo()

        Demo demo1 = demo + a

        Demo sssss = demo + 3

        Demo fsa = demo * 3

        Demo demo2 = demo / 9

        Demo demo3 = demo << a

        Demo demo4 = demo >> a

        Demo demo5 = demo++


        def i = 2 >>> 1

    }

    static class Demo {

        def plus(Demo demo) {
            output("Additive object")
            this
        }

        def plus(int s) {
            output("addition")
            this
        }

        def multiply(int a) {
            output("multiplication")
            this
        }

        def div(int a) {
            output("division")
            this
        }

        def leftShift(Demo demo) {
            output("<<operation")
            this
        }

        def rightShift(Demo demo) {
            output(">>operation")
            this
        }

        def next() {
            output("++operation")
            this
        }
    }
}

console output

No surprise no surprise!

Next, write one in combination with the thread class of the performance test framework:

 RequestThreadTimes requestThreadTimes = new RequestThreadTimes(FanLibrary.getHttpGet(""), 100);
        List<RequestThreadTimes> threads = requestThreadTimes * 100;
        new Concurrent(threads).start()

The multiply overload is as follows:

    /**
     * multiplication
     *
     * @param i
     * @return
     */
    public List<RequestThreadTimes> multiply(int i) {
        ArrayList<RequestThreadTimes> threads = new ArrayList<>(i);
        i.times {
            threads << this.clone();
        }
        threads
    }

Ha, ha, ha!

  • There's another big secret: Groovy can override the "." operator.
  • Solemnly declare: the article begins with the public number "FunTester", prohibits the third parties (except Tencent cloud) reprint and publish.

Selected technical articles

Selected non-technical articles

Posted by reddymade on Wed, 08 Jan 2020 06:51:45 -0800