gpt4 book ai didi

java - 如何拥有动态方法参数

转载 作者:行者123 更新时间:2023-12-01 14:24:28 25 4
gpt4 key购买 nike

可以说我有一个方法
public abstract void doMath(Holder h);
但我希望用户能够调整传入的类。像这样
public abstract void doMath(ClassOfPersonalChoice h);
我将如何去做。

感谢您的时间和精力。

PS:被传递的类包含一堆变量。
IE。

class ClassofPersonalChoice{
double hue = 0;
int R = 240;
int G = 10;
int B = 180;
}

最佳答案

如果你说

public abstract void doMath(Object h);

然后用户可以实现他想要的任何东西。

进阶思路:使用泛型/类型参数:
public abstract class MathClass<T> {

public abstract void doMath(T holder);

}
public class IntegerMathClass extends MathClass<ClassOfPersonalChoice> {

@Override
public void doMath(ClassOfPersonalChoice holder) {
// do something with the value
}

}

关于java - 如何拥有动态方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com