- 类型参数:
T
- 具有运算符重载的类型
public interface Operator<T>
运算符重载
- 从以下版本开始:
- 2.0
- API Note:
// 书写一个Int类,重载加法,减法和大于运算符 class Int implements Operator<Int> { int value; public Int(int value) { this.value = value; } @Override public Int plus(Int other) { return new Int(this.value + other.value); } @Override public Int minus(Int other) { return new Int(this.value - other.value); } @Override public boolean greaterThan(Int other) { return this.value > other.value; } public int getValue() { return this.value; } } Int a = new Int(3), b = new Int(4); // 二元运算 // c = a + b Int c = Operator.calc(a, "+", b); Assert.assertEquals(7, c.getValue()); // 二元运算 // c = a - b c = Operator.calc(a, "-", b); Assert.assertEquals(-1, c.getValue()); // 二元表达式运算 // c = a + (b - a) c = Operator.calc("? + (? - ?)", a, b, a); Assert.assertEquals(4, c.getValue()); // 布尔值运算 // flag = a > b boolean flag = Operator.cmp(a, ">", b); Assert.assertFalse(flag); // 布尔表达式运算 // flag = b > a && b - a > a flag = Operator.cmp("? > ? && ? - ? > ?", b, a, b, a, a); Assert.assertFalse(flag);
-
方法概要
修饰符和类型方法说明default boolean
重载运算符 &&default T
重载运算符 &default T
bitAndAssign
(T other) 重载运算符 &=default T
重载运算符 |default T
bitOrAssign
(T other) 重载运算符 |=default T
重载运算符 ~default T
重载运算符 ^default T
bitXorAssign
(T other) 重载运算符 ^=static <T extends Operator<T>>
T一元前置运算static <T extends Operator<T>>
T表达式运算(只支持二元运算)static <T extends Operator<T>>
T一元后置运算static <T extends Operator<T>>
T二元运算static <T extends Operator<T>>
boolean一元前置布尔值运算static <T extends Operator<T>>
boolean表达式布尔运算(只支持二元运算)static <T extends Operator<T>>
boolean二元布尔值运算default T
decPost()
重载运算符 --(后置)default T
decPre()
重载运算符 --(前置)default T
重载运算符 /default T
重载运算符 /=default boolean
重载运算符 ==default boolean
重载运算符 >=default boolean
greaterThan
(T other) 重载运算符 >default T
incPost()
重载运算符 ++(后置)default T
incPre()
重载运算符 ++(前置)default boolean
重载运算符 <=default boolean
重载运算符 <default T
重载运算符 -default T
minusAssign
(T other) 重载运算符 -=default T
重载运算符 %default T
重载运算符 %=default boolean
not()
重载运算符 !default boolean
重载运算符 !default boolean
重载运算符 ||default T
重载运算符 +default T
plusAssign
(T other) 重载运算符 +=default T
重载运算符 <<default T
重载运算符 <<=default T
重载运算符 >>default T
重载运算符 >>=default T
重载运算符 *default T
timesAssign
(T other) 重载运算符 *=default T
重载运算符 -(负号)default T
重载运算符 +(正号)default T
重载运算符 >>>default T
uShrAssign
(T other) 重载运算符 >>>=
-
方法详细资料
-
plus
重载运算符 +- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
minus
重载运算符 -- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
times
重载运算符 *- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
div
重载运算符 /- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
mod
重载运算符 %- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
plusAssign
重载运算符 +=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
minusAssign
重载运算符 -=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
timesAssign
重载运算符 *=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
divAssign
重载运算符 /=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
modAssign
重载运算符 %=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
incPre
重载运算符 ++(前置)- 返回:
- 运算结果
-
decPre
重载运算符 --(前置)- 返回:
- 运算结果
-
incPost
重载运算符 ++(后置)- 返回:
- 运算结果
-
decPost
重载运算符 --(后置)- 返回:
- 运算结果
-
unaryPlus
重载运算符 +(正号)- 返回:
- 运算结果
-
unaryMinus
重载运算符 -(负号)- 返回:
- 运算结果
-
and
重载运算符 &&- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
or
重载运算符 ||- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
not
default boolean not()重载运算符 !- 返回:
- 运算结果
-
eq
重载运算符 ==- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
notEq
重载运算符 !=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
lessThan
重载运算符 <- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
greaterThan
重载运算符 >- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
lessEq
重载运算符 <=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
greaterEq
重载运算符 >=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
bitReverse
重载运算符 ~- 返回:
- 运算结果
-
bitAnd
重载运算符 &- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
bitOr
重载运算符 |- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
bitXor
重载运算符 ^- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
shl
重载运算符 <<- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
shr
重载运算符 >>- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
uShr
重载运算符 >>>- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
bitAndAssign
重载运算符 &=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
bitOrAssign
重载运算符 |=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
bitXorAssign
重载运算符 ^=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
shlAssign
重载运算符 <<=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
shrAssign
重载运算符 >>=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
uShrAssign
重载运算符 >>>=- 参数:
other
- 另一个元素- 返回:
- 运算结果
-
calc
表达式运算(只支持二元运算)- 类型参数:
T
- 具有运算符重载的类- 参数:
exp
- 算数表达式(?为占位符)args
- 参数- 返回:
- 表达式运算结果
- API Note:
class Int implements Operator<Int> { ... } Int a = new Int(3), b = new Int(4); // 二元表达式运算 // c = a + (b - a) c = Operator.calc("? + (? - ?)", a, b, a);
-
calc
二元运算- 类型参数:
T
- 具有运算符重载的类- 参数:
a
- 元素aopr
- 运算符oprb
- 元素b- 返回:
- a opr b
- API Note:
class Int implements Operator<Int> { ... } Int a = new Int(3), b = new Int(4); // 二元运算 // c = a + b Int c = Operator.calc(a, "+", b);
-
calc
一元前置运算- 类型参数:
T
- 具有运算符重载的类- 参数:
opr
- 前置运算符a
- 元素a- 返回:
- opr a
- API Note:
class Int implements Operator<Int> { ... } Int a = new Int(3); // 一元前置运算 // c = -a Int c = Operator.calc("-", a);
-
calc
一元后置运算- 类型参数:
T
- 具有运算符重载的类- 参数:
a
- 元素aopr
- 后置运算符- 返回:
- a opr
- API Note:
class Int implements Operator<Int> { ... } Int a = new Int(3); // 一元后置运算 // c = a++ Int c = Operator.calc(a, "++");
-
cmp
表达式布尔运算(只支持二元运算)- 类型参数:
T
- 具有运算符重载的类- 参数:
exp
- 算数表达式args
- 参数- 返回:
- 表达式结果布尔值
- API Note:
class Int implements Operator<Int> { ... } Int a = new Int(3), b = new Int(4); // 布尔表达式运算 // flag = b > a && b - a > a boolean flag = Operator.cmp("? > ? && ? - ? > ?", b, a, b, a, a);
-
cmp
二元布尔值运算- 类型参数:
T
- 具有运算符重载的类- 参数:
a
- 元素aopr
- 运算符b
- 元素b- 返回:
- a opr b
- API Note:
class Int implements Operator<Int> { ... } Int a = new Int(3), b = new Int(4); // 二元布尔运算 // flag = a > b boolean flag = Operator.cmp(a, ">", b);
-
cmp
一元前置布尔值运算- 类型参数:
T
- 具有运算符重载的类- 参数:
opr
- 前置运算符a
- 元素a- 返回:
- opr a
- API Note:
class Int implements Operator<Int> { ... } Int a = new Int(3), b = new Int(4); // 一元前置布尔运算 // flag = !a boolean flag = Operator.cmp("!", a);
-