首页 > 编程知识 正文

Java重载运算符,java 操作符重载

时间:2023-05-05 09:32:42 阅读:158101 作者:2186

Ava重载运算符

. TG { border-collapse : collapse; border-spacing:0 }.tg td{font-family:Arial,sans-serif; font-size:14px; padding:10px 5px; border-style:solid; border-width:1px; overflow :隐藏; 魔兽世界:正规; border-color :黑色; }.tg th{font-family:Arial,sans-serif; font-size:14px; 字体权重:正规; padding:10px 5px; border-style:solid; border-width:1px; overflow :隐藏; 魔兽世界:正规; border-color :黑色; }.TG.TG-0 lax { text-align : left; vertical-align : top } manifold扩展依赖项插入到Java中,以提供无缝的运算符重载功能。 通过实现一个或多个预定义的运算符方法,可以为任何类型的类安全地提供算术、关系和单位运算符。 运算符方法可以直接在类中实现,也可以在本来无法使用扩展方法控制的类中实现。 例如,使用扩展方法Manifold可以编写以下代码,因为它为BigDecimal提供了运算符实现:

bigdecimal result=big value1big value 2; 通过单位表达式,可以更轻松地使用BigDecimal和其他类型。

bigdecimal result=3.14 BD * 10.75 BD; //`bd` makes BigDecimals单元和操作员超载的准确测量:

Length distance=65 mph * 3.2 hr; heatcapacitykboltzmann=1.380649 e-23rj/dk; 无论是算术运算符还是否定运算符,都可以通过实现以下一种或多种运算符方法来支持算术运算符:

算术

操作方法aBa.plus(b ) a - b a.minus(b ) b ) a * b a.times(b ) b ) a/b a.div(b ) b ) a % b a.rem(b ) b ) http://www.358

操作方法-a a.unaryMinsu (请注意,运算符方法不属于实现的类或接口。 相反,通过定义具有相同签名的方法,可以在结构上轻松实现。 根据参数类型,可以实现同一方法的多个不同版本。

这是一个演示如何实现运算符的简单示例。

public class point { publicfinalintx,y; publicpoint(intx,int y ) ) {this.x=x; this.y=y; }publicpointplus(pointthat ) returnnewpoint ) y that.y,y that.y; } vara=new point (1,2 ); varb=new point (3,4 ); var sum=a b; //point (4,6 )运算符方法是结构化的,因此可以定义多个plus )方法。

publicpointplus(int[]coord ) if ) coord.length!=2) thrownewillegalargumentexception (; }返回新点(y coord[1],y coord[1] ); }

关系运算符

可以使用ComparableUsing和/或Comparable接口的组合来实现关系运算符。

所有manifold.ext.API.comparableusing关系运算符都可以与comparable using接口一起实现,以提供运算符特定的API。

Booleancomparetousing(tthat,Operator op ); 其中Operator为enum,为关系运算符指定常数。

操作方式可以比较的是Impl比较的implaba.comparetousing(b,GT ) a.compareto ) b ) 0 a=b a.compareToUsing(b ) b,GE ) a.compareto )

可互换性using提供

供了compareToUsing()的默认实现,该默认实现委托给关系运算符的> , >= , <和<=子集的Comparable的compareTo()实现。 对于==和!=子集, ComparableUsing委托给该类型的equals()方法(稍后将详细介绍平等性)。 此行为适用于大多数类型,因此通常您只需要向您的类型的implements或extends子句中添加ComparableUsing即可ComparableUsing仅实现Comparable 。 因此,在Point示例中增加了关系运算符支持:

public class Point implements ComparableUsing { public final int x, y; public Point(int x, int y) {this.x = x; this.y = y;} public Point plus(Point that) { return new Point(x + that.x, y + that.y); } public int compareTo(Point that) { return x - that.x; }}

现在,您可以像这样轻松地比较“点”值:

if (pt1 >= pt2) ... java.lang.Comparable

如果您对支持==和!=不感兴趣,并且您的类型实现了Comparable接口,它将自动支持关系运算符的> , >= , <和<=子集。 例如,既java.lang.String和java.time.LocalDate实现compareTo()从方法Comparable ,这意味着它们可以在关系表达式中使用的:

String name1;String name2;...if (name1 > name2) {...} LocalDate date1;LocalDate date2;...if (date1 > date2) {...} 还请参见: 平等经营者

要实现关系运算符的==和!=子集,必须实现ComparableUsing接口。 默认情况下, ComparableUsing委托给您类型的equals()方法,但是您可以通过重写CopmarableUsing实现中的CopmarableUsing equalityMode()方法来轻松覆盖此行为。 EqualityMode枚举提供了可用的模式:

/** * The mode indicating the method used to implement {@code ==} and {@code !=} operators. */enum EqualityMode{ /** Use the {@code #compareTo()} method to implement `==` and `!=` */ CompareTo, /** Use the {@code equals()} method to implement `==` and `!=` (default) */ Equals, /** Use {@code identity} comparison for `==` and `!=`, note this is the same as Java's normal {@code ==} behavior } */ Identity}

根据您的CompareToUsing#equalityMode()实现返回的EqualityMode , ==和!=运算符使用以下方法进行编译:

操作方式 Equals <small>(默认)</ small> CompareTo Identity a == b a.equals(b) a.compareToUsing(b, EQ) a == b a != b !a.equals(b) a.compareToUsing(b, NE) a != b

注意:歧管为==和!=生成有效的, 空安全的代码。 例如,使用Equals模式的a == b编译为:

a == b || a != null && b != null && a.equals(b)If you need something more customized you can override compareToUsing() with your own logic for any of the operators, including == and != . To enable == on Point more effectively, you can accept the default behavior of ComparableUsing and implement equals() : public boolean equals(Object that) { return this == that || that != null && getClass() == that.getClass() && x == ((Point)that).x && y == ((Point)that).y;}

请注意,如果实现equals() ,请始终考虑实现hashCode() ,否则在与Map和其他数据结构一起使用时,您的类型可能无法正常工作:

public int hashCode() { return Objects.hash(x, y); }

有时最好使用CompareTo模式。 例如, Rational , BigDecimal和BigInteger的==和!=实现使用CompareTo模式,因为在那些类中, compareTo()反映了它们所建模数字的面值的相等性,例如1.0 == 1.00,即在许多用例中的期望行为。 在那种情况下,只需重写equalityMode()以返回CompareTo :

@Overridepublic EqualityMode equalityMode() { return CompareTo;} 还请参见: 单位经营者

单位或“绑定”操作对于歧管框架是唯一的。 它们提供了功能强大且简洁的语法,可以应用于广泛的应用程序。 您可以使用prefixBind()和postfixBind()方法实现运算符:

操作方式 后缀绑定 前缀绑定 ab b.postfixBind(a) a.prefixBind(b)

如果类型a工具R prefixBind(B)其中B距离的类型分配的b ,则ab编译作为方法调用a.prefixBind(b)具有式R 否则,如果类型b器具R postfixBind(A)其中A是从分配的类型a ,然后ab编译作为方法调用b.postfixBind(a)具有式R

此功能启用以下表达式:

Mass m = 5 kg;Force f = 5 kg * 9.8 m/s/s;for (int i: 1 to 10) {...}

阅读更多有关单位表达式的信息 。

扩展方法运算符

使用扩展方法,您可以为原本无法控制的类提供操作员实现。 例如,Manifold为BigDecimal和BigInteger提供了运算符扩展。 这些扩展是在manifold-science依赖中实现的。

这是BigDecimal的+扩展名:

@Extensionpublic abstract class ManBigDecimalExt implements ComparableUsing { /** Supports binary operator {@code +} */ public static BigDecimal plus(@This BigDecimal thiz, BigDecimal that) { return thiz.add(that); } ...}```Now you can perform arithmetic and comparisons using operator expressions:```javaif (bd1 >= bd2) { BigDecimal result = bd1 + bd2; . . .}

请注意, manifold-science和manifold-collections模块广泛使用运算符重载和单位表达式。

翻译自: https://jaxenter.com/manifold-operator-overloading-java-163232.html

java重载运算符

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。