重载一时爽,一直重载一直爽。

最近在读《Groovy in action》一本书重新复习了Groovy的一些语法特性,迷恋上这个重载操作符的功能,坚持爽的不要要的。分享一个Demo。

由于Groovy语法跟Java差别略大但又基本完全兼容Java语法,这个Demo依然以Java语法写出来,方便大家理解。

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("加法对象")
this
} def plus(int s) {
output("加法")
this
} def multiply(int a) {
output("乘法")
this
} def div(int a) {
output("除法")
this
} def leftShift(Demo demo) {
output("<<操作")
this
} def rightShift(Demo demo) {
output(">>操作")
this
} def next() {
output("++操作")
this
}
}
}

控制台输出

惊不惊喜意不意外!

下面结合性能测试框架的thread类写一个:

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

乘法重载如下:

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

哈,哈,哈!

  • 还有一个大秘密:Groovy连操作符“.”也能重写。

  • 郑重声明:文章首发于公众号“FunTester”,禁止第三方(腾讯云除外)转载、发表。

技术类文章精选

非技术文章精选

Groovy重载操作符的更多相关文章

  1. 《精通C#》索引器与重载操作符(11.1-11.2)

    1.索引器方法结构大致为<modifier><return type> this [argument list],它可以在接口中定义: 在为接口声明索引器的时候,记住声明只是表 ...

  2. C++ 重载操作符与转换

    <C++ Primer 4th>读书笔记 重载操作符是具有特殊名称的函数:保留字 operator 后接需定义的操作符号. Sales_item operator+(const Sales ...

  3. 解释清楚c++的重载操作符【用自己的话,解释清楚】

    C++中对于内置的变量及标准库中常见的类定义类常见的操作符含义,对于自定义的类也可以通过关键字operate 重载操作符的含义. C++中支持重载的目的 诚然操作符的重载可以通过使用函数实现同样的功能 ...

  4. 重载操作符 operator overloading 学习笔记

    重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...

  5. [019]转--C++ operator关键字(重载操作符)

    原博客:http://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html operator是C++的关键字,它和运算符一起使用,表示一 ...

  6. VC6.0中重载操作符函数无法访问类的私有成员

    整理日: 2015年03月18日 在 C++ 中,操作符(运算符)可以被重载以改写其实际操作.同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成 ...

  7. C++高精度运算类bign (重载操作符)

    大数据操作,有例如以下问题: 计算:456789135612326542132123+14875231656511323132 456789135612326542132123*14875231656 ...

  8. C++ operator关键字(重载操作符)(转)

    operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...

  9. C++中operator关键字(重载操作符)

    operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...

随机推荐

  1. Python 2.X 版本 600行入门基础

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  2. HDU 4417 Super Mario 主席树查询区间小于某个值的个数

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  3. iptables禁止ssh端口

    只允许在192.168.62.1上使用ssh远程登录,从其它计算机上禁止使用ssh #iptables -A INPUT -s 192.168.62.1 -p tcp --dport 22 -j AC ...

  4. angular.extend vs angular.copy

    1.angular.copy angular.copy(source, [destination]);  // source: copy的对象. 可以使任意类型, 包括null和undefined. ...

  5. 5 分钟入门 Google 最强NLP模型:BERT

    BERT (Bidirectional Encoder Representations from Transformers) 10月11日,Google AI Language 发布了论文 BERT: ...

  6. python开启GPU加速

    看了好多教程都提到了使用 os.environ["CUDA_VISIBLE_DEVICES"] = "1" 或者export CUDA_VISIBLE_DEVI ...

  7. 以P2P网贷为例互联网金融产品如何利用大数据做风控?

    以P2P网贷为例互联网金融产品如何利用大数据做风控?   销售环节 了解客户申请意愿和申请信息的真实性:适用于信贷员模式. 风控关键点 亲见申请人,亲见申请人证件,亲见申请人签字,亲见申请人单位. 审 ...

  8. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...

  9. MySQL高级配置

    参考文章:http://www.jb51.net/article/47419.htm https://blog.csdn.net/waneto2008/article/details/52502208 ...

  10. 通过页码直接跳转 html

    <?php namespace Admin\TagLib; class BootstrapPage{ public $firstRow; // 起始行数 public $listRows; // ...