重定义/过载类型铸造 [英] Redefine/overload type casting
问题描述
每个对象都有一个toString方法继承自它扩展的Object类,它可以重载。有没有办法做一个类似的东西与对象?这里是我想做的:
int a = n + 1;其中n实际上是一个对象,但当用作一个int具有特定的值(例如。在构造函数中指定)。
Every object has a toString method inherited from the Object class it extends, which can be overloaded. IS there a way to do a similar thing with objects? Here is what I want to do: int a=n+1; where n is actually an object, but when used as an int has a specific value (eg. specified in the constructor).
推荐答案
这称为运算符重载。你不能在Java中做。据我所知,它被设计所禁止。我想你是来自C ++或C#,这是可能的。
This is called operator overloading. You cannot do it in Java. And it is disallowed by design as far as I'm aware. I guess you're coming from C++ or C#, where this is possible.
http://en.wikipedia.org/wiki/Operator_overloading
这篇关于重定义/过载类型铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!