简体   繁体   English

自动从对象转换为基本类型

[英]Automatic cast from object to primitive type

I have been looking for a transparent way to cast an object to a primitive type, something like: 我一直在寻找一种透明的方式将对象转换为原始类型,例如:

class Example {
    double number;

    public Example(double number) {
        this.number = number;
    }

    // this or something similar to this
    public toDouble() {
        return number;
    }

    ...
}

Example ex = new Example(18.0);
double number = ex;

After a few searches i am almost sure that it does not exists, but if there would be a big help. 经过几次搜索后,我几乎可以确定它不存在,但是如果有很大帮助的话。

Update 1 更新1

Excuse me if the questions was not very clear. 如果问题不是很清楚,请原谅。

I am developing a code editor for an internal tool: the idea is write a program with a reduced set of java instructions, translate to java and compile. 我正在为内部工具开发代码编辑器:这个想法是用减少的Java指令集编写程序,转换为Java并进行编译。

I want avoid that the editor have to make a lot of times the same casts or call same calls, and the exprexions sometimes will be very complex to translate. 我想避免编辑器不得不多次进行相同的强制转换或调用相同的调用,并且有时表达式的翻译非常复杂。

A better example could be: 一个更好的例子可能是:

the simplified code 简化代码

Example ex = new Example(18.0);
Example ex2 = new Example(23.0);

Example mean = Mean(ex + 1.0, ex2, 3.0);

(i would prefer avoid Example mean = Mean(ex.toDouble() + 1.0, ex2.toDouble(), 3.0); ) (我宁愿避免Example mean = Mean(ex.toDouble() + 1.0, ex2.toDouble(), 3.0);

and the function in java 和Java中的功能

double Mean(double... numbers)

All the primitive types in java have their wrappers: Integer , Double etc. Java中的所有原始类型都有其包装器: IntegerDouble等。

According to Java tutorials: 根据Java教程:

Converting an object of a wrapper type ( Integer ) to its corresponding primitive ( int ) value is called unboxing. 将包装器类型( Integer )的对象转换为其对应的原始( int )值称为拆箱。

And this is done pretty automatically, so I am not sure which object else would you like to "cast" to primitive? 而且这是自动完成的,所以我不确定您还想将哪个对象“投射”到原始对象? You can use a method like your toDouble() , which will return primitive, but it won't be casting at all. 您可以使用类似toDouble()的方法,该方法将返回原始值,但根本不会进行强制转换。

You can use the wrapper of double, Double . 您可以使用double的包装Double

Double d = new Double(10.3);
double d1 = d; // This will be auto(un)boxing and not casting

transparent way to cast an object to a primitive type 将对象转换为原始类型的透明方法

double number = ex; this is illegal as ex is of type Example and number is of the primitive type double . 这是非法的,因为ex是Example类型,而number是原始类型double This really doesn't make sense. 这真的没有道理。

You either need to use toDouble() , or completely remove the Example class and use the wrapper class Double as mentioned above. 您或者需要使用toDouble() ,或者完全删除Example类并使用包装器类Double,如上所述。

You cannot cast, and little confusing what you are trying to do. 您无法投射,并且几乎不会混淆您要尝试执行的操作。

If you need double then , call toDouble() 如果需要double,则调用toDouble()

double number = ex.toDouble();

or even, as some one commented, use the Double class constructor Double(double value) 甚至有人评论说,使用Double类构造函数Double(double value)

  Double wrapper = new Double(18.0);  //class
  double primitiveDouble =wrapper;    // primitive

There is no analogy to C#`s implicit modifier. 没有比喻C#的隐式修饰符。 In general there are less syntax sugar in Java then in .NET. 通常,Java中的语法糖少于.NET中的语法糖。 I dont this this is a problem. 我不这是一个问题。 You can look inside sources of Integer, Double, Float etc. All they have methods like floatValue(), longValue() and so on. 您可以查看Integer,Double,Float等的源代码。它们都有诸如floatValue(),longValue()等方法。 Making casts explicit improves code readability. 使强制类型转换明确可提高代码可读性。 While implicit casts, like available in C#, are very hard to understand. 尽管隐式强制转换(如C#中可用的)很难理解。 You can make your own method yourTypeValue(). 您可以将自己的方法设为yourTypeValue()。 This is a sort of Java convention. 这是一种Java约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM