替换/覆盖Java的“本地".上课吗 [英] Replacing/Overriding Java "native" Classes?

查看:77
本文介绍了替换/覆盖Java的“本地".上课吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BigDecimal s,并且我要求除以0不应导致ArithmeticException,而应返回0(古怪的数学运算).

I'm working with BigDecimals and I have the requirement that a division by 0 should not result in an ArithmeticException, but in returning 0 instead (weird business math).

这是一个相当新的要求,我们已经有很多代码,在很多地方都使用BigDecimal.我不想遍历所有这些地方并实施零检查.这对于使用第三方库的第三方库也无济于事,该库可能在​​内部使用BigDecimal s并抛出ArithmeticException代替.

This is a rather new requirement and we already have quite a bit of code, which uses BigDecimals in a lot of places. I don't want to go through all these places and implement zero checks. This also would not help me with 3rd party libraries, which might internally use BigDecimals and will throw an ArithmeticExceptioninstead.

我还想设置默认的精度/比例并更改compareTo方法以能够忽略小的舍入误差.

I'd also like to set a default precision/scale and change the compareTo method to able to ignore small rounding errors.

由于所有这些全局更改将创建许多样板"代码,因此我想到了更改BigDecimal的实现的想法.之前,我已经为其他3rd Party类完成了此操作,以便自己修复某些错误.

Because of all these global changes which would create a lot of "boilerplate" code, I came up with the idea to change the implementation of BigDecimal. I've already done this before for other 3rd party classes to fix certain bugs myself.

我通过在与第3方类相同的包中创建一个具有相同名称的类来替换了这些类,并且由于外部jar文件将在我自己的类之后加载,因此我能够替换它们.

I replaced those classes by creating a class with the same name in the same package like the 3rd party class, and because the external jar files will be loaded after my own classes, I was able to replace them.

但是创建java.math.BigDecimal并没有帮助我,因为似乎本地" Java类甚至在我自己的类之前就已加载.

But creating a java.math.BigDecimal didn't help me, because it seems that the "native" Java classes are loaded even before my own classes.

让我们假设我真的希望应用程序中的每个BigDecimal都不同,我该如何替换官方" BigDecimal?我可以这样做吗?还有其他我现在没有想到的技术问题吗?

Let's assume that I really want every single BigDecimal in my application to work a bit different, how would I be able to replace the "official" BigDecimal? Am I allowed to do that, and could there be some other, technical problems I didn't think of now?

推荐答案

您必须将您的类放在.关于实际执行此操作的智慧(即您的更改将影响整个 jvm)...

You have to put your classes in the "bootstrap" classpath if you want to override builtin classes. as to the wisdom of actually doing this (i.e. your changes will affect the entire jvm)...

这篇关于替换/覆盖Java的“本地".上课吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆