【问题标题】:Generics with compile time vs Runtime with Class Type used in frameworks in javajava框架中使用的具有编译时间的泛型与具有类类型的运行时
【发布时间】:2013-09-13 09:24:08
【问题描述】:

我写了三个程序,一个使用泛型,另一个不使用泛型

使用 isAssignableFrom

public class ObjectSpecificCondition {
    private Class classType;

    public boolean check(Object obj) {
        boolean check = ((DateObject) obj).getLocalizationdate();
    }

    public ObjectSpecificCondition(Class classType) {
        this.classType = classType;
    }

    boolean checkTypeSpecific(Object busineesObject) {
        if (classType.isAssignableFrom(busineesObject.getClass())) {
            return true;
        }
        else{
            throw new Exception();
        }

    }

使用 instanceOf

class ObjectSpecificCondition1 {

    public boolean check(Object busineesObject) {
        boolean check ;

        if(busineesObject instanceof DateObject){
            check= ((DateObject) busineesObject).getLocalizationdate();
        }
        else{
             throw new IllegalArgumentException();
        }
        return check;
    }

    }

使用泛型

class ObjectSpecificConditionGenerics<T extends DateObject> {
    private T classTypeGenerics;

    public boolean check(T genericsobj) {
        genericsobj.getLocalizationdate();

    }

}

业务对象规则

class DateObject {
    boolean getLocalizationdate() {
        // return true Or False according to business logic
    }
}

Main test
 public static void main(String[] args) throws Exception {
        DateObject mockdateObject = new DateObject();
        // caseI: No generics used caseI works fine no exception is generated but more lines of code to write
        ObjectSpecificCondition mockanalysis = new ObjectSpecificCondition(DateObject.class);

        if (mockanalysis.checkTypeSpecific(mockdateObject)) {
            mockanalysis.check(mockdateObject);

        }
     // caseII:No generics used caseII throws exception in run time .More lines of code to write.It can not capture incompatible type at compile time 
        ObjectSpecificCondition mockanalysis1 = new ObjectSpecificCondition(String .class);
        DateObject mockdateObject1 = new DateObject();
        if (mockanalysis.checkTypeSpecific(mockdateObject1)) {
            mockanalysis.check(mockdateObject1);

        }
       // caseIII;Generics used and line of code is reduced to less 
        ObjectSpecificConditionGenerics mockgenerics=new ObjectSpecificConditionGenerics() ;
        mockgenerics.check(mockdateObject);

        // caseIV;Generics used and line of code is reduced to less and error for compataible object is generated at compile time 
        ObjectSpecificConditionGenerics mockgenerics1=new ObjectSpecificConditionGenerics() ;
        String mockstring=new String();
        mockgenerics.check(mockstring); // it is captured at compile time ,i think its good to catch at compile time then to pass it at run time 

    }
}

我在使用三种方法的框架中看到。我想要更多地确认哪一种可以是最好的?使用泛型可以减少代码行并且在编译时会产生错误。但是,另一种方法也被更多地使用。我想得到更深入的答案。请提供任何帮助

【问题讨论】:

  • 我没有足够的经验来真正回答这个问题,但我猜,由于泛型相对较新,非泛型库要么较旧,要么由更熟悉该风格的人编写。跨度>

标签: java generics compiler-errors runtime-error


【解决方案1】:

泛型和非泛型版本应该相同,唯一的区别是类型参数和强制类型转换。非泛型版本应该是泛型版本的类型擦除。任何泛型代码都可以通过类型擦除转换转换为等效的非泛型代码。

不使用泛型

public class ObjectSpecificCondition {
    private Class classType;

    public boolean check(DateObject obj) {
        boolean check = obj.getLocalizationdate();
    }

    public ObjectSpecificCondition(Class classType) {
        this.classType = classType;
    }

    boolean checkTypeSpecific(Object busineesObject) {
        if (classType.isInstance(busineesObject)) {
            return true;
        }
        else{
            throw new Exception();
        }

    }
}

使用泛型

public class ObjectSpecificCondition<T extends DateObject> {
    private Class<T> classType;

    public boolean check(T obj) {
        boolean check = obj.getLocalizationdate();
    }

    public ObjectSpecificCondition(Class<T> classType) {
        this.classType = classType;
    }

    boolean checkTypeSpecific(Object busineesObject) {
        if (classType.isInstance(busineesObject)) {
            return true;
        }
        else{
            throw new Exception();
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多