简体   繁体   English

如何为一个已经存在的类创建一个方法,以便它使用一个变量,从该变量中将其称为参数?

[英]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 for Integer and I would like it to work this way: x.add(y) , where x and y are Integers . 假设我想为Integer创建一个add方法,并希望它以这种方式工作: x.add(y) ,其中xyIntegers 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. 明确地说,我首先质疑它的原因是,我需要创建一个应该能够做到的通用类,例如,为IntegerBigInteger都加法,并且我不想创建检查哪个班是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作为泛型类型, IntegerBigInteger都是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的类,因为Integerfinal ,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.

相关问题 如何从用户那里获取任意数量的参数并将其传递给使用varargs(variable arguments)的方法? - How to take any number of arguments from the user and pass them to a method which uses varargs( variable argument )? 我的方法(正在被另一个类调用)如何从该类获取变量而不将其作为参数传递? - How can my method, which is being called by another class, obtain a variable from that class without passing it as a parameter? 如何存根从另一个类调用的类中的方法 - how to stub a method in a class which is called from another class 如何从声明了变量的方法调用的方法中引用方法范围的变量? - How to refer to a variable with method scope from a different method called from method in which variable is declared? 如何在一个类(JPanel)中正确引用刷新方法,因此可以从另一个类中的方法调用它? - How to get proper reference to refreshing method in one class(JPanel), so it can be called from method in another class? 如何创建从动态类名访问变量的方法? - How can I create a method which accesses a variable from a dynamic class name? 如何创建静态类的实例以便可以从单独的类中调用它? - How to create an instance of a static class so it can be called from a separate class? @ControllerAdvice,如何获取调用此方法的类 - @ControllerAdvice, how to get the class which called this method 如何编写使用来自另一个类的参数列表的@Parameters方法? - How to write @Parameters method which uses parameter list from another class? Mockito:如何返回调用模拟方法的参数作为响应? - Mockito: how to return the argument, which the mock method was called with, as response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM