java代码抽象,泛型,我不理解这里的泛型java代码

aoyhnmkz  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(378)
public abstract class BaseDataItem<V, T, ThisType extends BaseDataItem<V, T, ThisType>> implements DataItem<V, T> { }

    public abstract class SingularDataItem<Value, T> extends BaseDataItem<Value, T, SingularDataItem<Value, T>> {

        @Override
        protected int buildHashCode(HashCodeBuilder builder) {
            return builder.toHashCode();
        }

        @Override
        protected boolean isEquivalent(SingularDataItem<Value, T> o) {
            return true; // the parent class already verifies that the object is the same class
        }
    }
o3imoua4

o3imoua41#

我试过了,返回的结果是真的,这意味着无论我为类数据创建多少不同的对象,它们都是相等的,因此我不知道为什么要编写这样的代码,也许可以从使用这些代码的域的Angular 来解释这些代码。

public class Test {

public static void main(String[] args) {

    SingularDataItem<String , String> singularDataItem = new Data("testing");
    SingularDataItem<String , String> singularDataItem2 = new Data("testing2");
    System.out.println(singularDataItem.isEquivalent(singularDataItem2));
}}

class Data extends SingularDataItem<String , String>{
private String name;

public Data(String name) {
    this.name = name;
}

public String getName() {
    return name;
}}

abstract class BaseDataItem<V, T, ThisType extends BaseDataItem<V, T, ThisType>> implements DataItem<V, T> {

int buildHashCode(HashCodeBuilder builder){
    return builder.toHashCode();
}

boolean isEquivalent(SingularDataItem<V, T> o){return true;}}

abstract class SingularDataItem<Value, T> extends BaseDataItem<Value, T, SingularDataItem<Value, T>> {

@Override
protected int buildHashCode(HashCodeBuilder builder) {
    return builder.toHashCode();
}

@Override
protected boolean isEquivalent(SingularDataItem<Value, T> o) {
    return true; // the parent class already verifies that the object is the same class
}}

相关问题