[英]How to create a method for an already existing class so that it uses a variable, from which it is called as an argument?
Update: thank you, now I understand that it is impossible to do what is written below. 更新:谢谢,现在我知道不可能做下面写的事情。
Let's say I want to create an
add
method forInteger
and I would like it to work this way:x.add(y)
, wherex
andy
areIntegers
.假设我想为
Integer
创建一个add
方法,并希望它以这种方式工作:x.add(y)
,其中x
和y
是Integers
。 Is there a possibility to do this?有可能这样做吗? The only thing I can think of is to create a different class that extends Integer and add a method for it, but is there any other way?
我唯一想到的就是创建一个不同的类来扩展Integer并为其添加一个方法,但是还有其他方法吗?
Just to be clear, the reason I was questioning it in the first place is that I need to create a generic class that should be able to do, for example, addition both for Integer
and BigInteger
and I don't want to make if cases that check what class is T
to do that. 明确地说,我首先质疑它的原因是,我需要创建一个应该能够做到的通用类,例如,为
Integer
和BigInteger
都加法,并且我不想创建检查哪个班是T
做到这一点。 So I thought it would be useful if it was possible to do the above. 因此,我认为如果可以执行上述操作将很有用。 Now it is still unclear for me how to do that.
现在我仍然不清楚该怎么做。 Sorry for such a confusing question.
很抱歉这样一个令人困惑的问题。
PS I'm new to Java and also not a native English speaker, so I understand that the question may be very silly or has already been answered, if that's the case, I'm sorry. PS:我是Java的新手,也不是讲英语的母语的人,所以我知道这个问题可能很愚蠢,或者已经回答了,如果是这样,对不起。
I have an unclear question. 我有一个不清楚的问题。 If you want to create an add method inside an existing Integer class, you can not create it because it is final.
如果要在现有Integer类中创建add方法,则不能创建它,因为它是最终的。 Instead, you can create an add method within your own class.
相反,您可以在自己的类中创建一个add方法。
public int add (Integer a, Integer b) {
return a + b;
}
UPDATED: 更新:
Just to be clear, the reason I was questioning it in the first place is that I need to create a generic class that should be able to do, for example, addition both for Integer and BigInteger and I don't want to make if cases that check what class is T to do that.
明确地说,我首先质疑它的原因是,我需要创建一个应该能够做到的通用类,例如,为Integer和BigInteger都加法,并且我不想创建检查哪个班是T做到这一点。
Why not use java.lang.Number
as generic type, Integer
and BigInteger
are both subclasses of java.lang.Number
. 为什么不使用
java.lang.Number
作为泛型类型, Integer
和BigInteger
都是java.lang.Number
子类。
No, you cannot add a method to Integer
, nor can you create a class that extends Integer
because Integer
is final
, final classes cannot be inherited in Java. 不,您不能向
Integer
添加方法,也不能创建扩展Integer
的类,因为Integer
是final
,final类不能在Java中继承。
If you just want to add two Integer
objects, just use +
because there is auto boxing and unboxing in Java, check this out: https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7 . 如果您只想添加两个
Integer
对象,请使用+
因为Java中具有自动装箱和拆箱功能,请查看以下内容: https : //docs.oracle.com/javase/specs/jls/se8/html/jls-5 .html#jls-5.1.7 。
You can create util class to provide static method to work around this: 您可以创建util类以提供静态方法来解决此问题:
class Integers {
public static Integer add(Integer i1, Integer i2) {
return i1 + i2;
}
}
By the way, you can add extension methods in Kotlin, where you can do things like 1.plus(2)
to perform plus operations. 顺便说一句,您可以在Kotlin中添加扩展方法,在其中您可以执行
1.plus(2)
操作来执行plus操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.