简体   繁体   中英

Java code abstraction, generics, I don't understand the generics java code here

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
        }
    }

Well I tried this and the output returned is true that means no matter how many different objects I create for the class Data they all will always be equal and hence I don't know why a code like this is written maybe this code can be explained when looked through the perspective of the domain where this code is used.

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
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM