Java-创建伪本地类型 [英] Java - Creating a Pseudo Native Type

查看:91
本文介绍了Java-创建伪本地类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在股票交易中,数量通常是整数(例如5x股,10x期权等).使用加密货币时,数量就是分数(例如0.01比特币).在这两种情况下,通常都有一个最小单位(例如100x份额的倍数).

In stock trading, quantities are usually integers (e.g. 5x shares, 10x options, etc). With cryptocurrencies, quantities are fractions (e.g. 0.01 bitcoin). In both scenarios, there is typically a minimum unit (e.g. multiples of 100x shares).

我想将此逻辑包装在Quantity类中.但是:

I would like to wrap this logic in a Quantity class. However:

  • Java本机类型(例如double)是 final ,因此我无法扩展它们
  • Java不支持运算符重载,因此运算法则很丑
  • Java不支持typedef,所以我不能用Quantity类型包装double
  • Java native types (e.g. double) are final, so I cannot extend them
  • Java does not support operator overloading, so arithmetic will be ugly
  • Java does not support typedefs, so I cannot wrap a double with a Quantity type

所以我想我的问题是,如果我想创建类似于本机类型的东西(很多实例,按值传递,大量算术运算),是否有经典" Java解决方案?从内存来看,C#具有struct类型,该类型是按值传递的,在Java中是否有类似的东西?

So I guess my question is this, if I want to create something akin to a native type (lots of instances, pass-by-value, lots of arithmetic), is there a "classic" Java solution? From memory, C# has a struct type which is pass-by-value, is there something similar in Java?

谢谢

是否可以从C ++代码导入本机类型,并有效地绕过Java?

Is it possible to import a native type from C++ code, and effectively bypass Java?

推荐答案

是的,可以在Java中声明类型定义,并保证类型正确性,但不存在声明新的语法复杂性和运行时开销的问题. Java类.

Yes, it is possible to declare typedefs in Java, with a guarantee of type-correctness, but without the syntactic complexity and run-time overhead of declaring a new Java class.

作为用户,您可以通过编写类型注释并在编译时使用注释处理器来做到这一点.例如,您将写@Quantity int表示数量,并使用javac -processor QuantityProcessor MyFile.java进行编译.您可以对@Quantity int使用任何算术运算,并且如果将常规int@Quantity int混合使用,或者如果存在其他错误(例如,值不在正确范围内),编译器将发出警告.是100的倍数.

As a user, you can do this by writing a type annotation and using an annotation processor at compile time. For example, you would write @Quantity int for quantities, and you would compile with javac -processor QuantityProcessor MyFile.java. You can use any arithmetic operation on a @Quantity int, and the compiler issues a warning if you mix regular ints with @Quantity ints, or if there are other errors such as a value not being in the right range or not being a multiple of 100.

有人需要定义类型注释和注释处理器,并且由于您的要求是唯一的,因此可能需要创建自己的注释. 检查器框架是一种易于创建类型注释和注释处理器的框架. (披露:我是一名开发人员.)

Someone needs to define the type annotations and the annotation processor, and because your requirements are unique, you will probably need to create your own. One framework that makes it easy to do create type annotations and annotation processors is the Checker Framework. (Disclosure: I am a developer.)

Checker框架手册的以下部分可能特别有用.

The following parts of the Checker Framework manual may be particularly helpful.

  • Type aliases and typedefs
  • Fake enumerations
  • How to create a new checker

这篇关于Java-创建伪本地类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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