"的正确用法-6ren"> "的正确用法-我不熟悉“泛型”。是否正确使用“”?您是否同意使用泛型后的代码更好? Before using Generics ===========================================-6ren">
gpt4 book ai didi

java - ""的正确用法

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:04 25 4
gpt4 key购买 nike

我不熟悉“泛型”。是否正确使用“<T extends SuperClass>”?您是否同意使用泛型后的代码更好?

Before using Generics
=================================================

public abstract class SuperSample {

public void getSomething(boolean isProcessA) {
doProcessX();
if(isProcessA){
doProcessY(new SubASample());
}else{
doProcessY(new SubBSample());
}
}

protected abstract void doProcessX();

protected void doProcessY(SubASample subASample) {
// Nothing to do
}

protected void doProcessY(SubBSample subBSample) {
// Nothing to do
}

}

public class SubASample extends SuperSample {

@Override
protected void doProcessX() {
System.out.println("doProcessX in SubASample");
}

@Override
protected void doProcessY(SubASample subASample) {
System.out.println("doProcessY in SubASample");
}

}

public class Sample {

public static void main(String[] args) {
SubASample subASample = new SubASample();
subASample.getSomething(true);
}

}

After using Generics
=================================================

public abstract class SuperSample {

public void getSomething(boolean isProcessA) {
doProcessX();
if(isProcessA){
doProcessY(new SubASample());
}else{
doProcessY(new SubBSample());
}
}

protected abstract void doProcessX();

protected abstract <T extends SuperSample> void doProcessY(T subSample);

}

public class SubASample extends SuperSample {

@Override
protected void doProcessX() {
System.out.println("doProcessX in SubASample");
}

@Override
protected <T extends SuperSample> void doProcessY(T subSample) {
System.out.println("doProcessY in SubASample");
}

}

public class Sample {

public static void main(String[] args) {
SubASample subASample = new SubASample();
subASample.getSomething(true);
}

}

最佳答案

如果您想做我认为您想做的事,我认为这不是正确的方法 (*)。如果你希望每个子类都需要实现一个方法来处理它自己的类型,那么你可以使用 CRTP技巧:

abstract class Super<S extends Super<S>> {
abstract void process(S s);
}

class SubA extends Super<SubA> {
void process(SubA s){ /* do something */ }
}

class SubB extends Super<SubB> {
void process(SubB s){ /* do something */ }
}

请注意,此模式强制执行子类的通用签名,例如class SubA extends Super<SubB>不会编译。

Java 本身在 java.lang.Enum 中使用了这个技巧, 顺便说一下。

(*)如果这不是您要强制执行的行为,请说明。

关于java - "<T extends SuperClass>"的正确用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5030578/

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