首页 > 解决方案 > 获取不同类对象的通用方法

问题描述

我有 3 个类(技术规范、包和功能)对象,它们都共享相同的字段。字段很大,而不是重复设置每个字段的字段 3 次(最终看起来像重复),我想将类对象传递给一个使用通用对象设置对象字段的方法。

我尝试将类对象作为泛型传递,但后来我无权访问它的成员。这是我试过的

  Packages packagesFeatures = new Packages();
  TechSpecs techSpecsFeature = new TechSpecs();
  packagesFeatures  =  addFeatures(Packages.class, packagesFeatures, vehFeatures);
  techSpecsFeature  = addFeatures(TechSpecs.class, techSpecsFeature, vehFeatures);

然后

    private <T> T addFeatures(Class<T> clazz, T obj,  VehicleFeature vehFeatures) {

    T inst = null;

    try {
        inst = clazz.getDeclaredConstructor().newInstance();

    } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        e.printStackTrace();
    }
    if (inst instanceof Packages) {
      obj = (T) new Packages();


    }
    if(inst instanceof TechSpecs){
        obj = (T) new TechSpecs();
    }

    if(inst instanceof Features){
        obj = (T) new Features();
    }


    //then somthing like:
    //obj.setFeatureId(vehFeatures.getFeatureId());
    // obj.setFeatureKey(vehFeatures.getFeatureKey());
    // obj.setFeatureCode(vehFeatures.getFeatureCode());

    return obj;

编辑 3 个类中的每一个都扩展了 BaseFeatures

public abstract class BaseFeatures {
private String featureId;
private String featureKey;
private String featureCode;
private String subSectionId;
private String subSectionName;
private String featureIdName;
private Integer subSectionRank;
private Integer featureImgClassificationId;
private String featureImgClassification;
private boolean has3DAnimation;
private String sectionId;
private String searchKeys;
private String description;
private String featureName;
private double featureRank;
private String geoId;
private String ecc;
private String specSegments;
private String featureIconType;
private String featureIconText;
private double featureValue;
private boolean standardCertain;
private boolean built;
private List<String> featureKeyAnswers;
private boolean isNumeric;
private boolean adasFeature;
private List<String> icCodeAnswers;
private String featureKeyNoBrand;
private List<StyleInfo> styles;
private List<String> optionCodes;
private List<String> changeOptions;

//getter 和 setter。

这是其中一堂课。

public class TechSpecs extends BaseFeatures {

private String techSpecs;


public void setTechSpecs(String techSpecs) {
    this.techSpecs = techSpecs;
}

public String getTechSpecs(){
    return techSpecs;
}

}

所有这些字段都需要在所有 3 个类的类对象中设置

编辑 2

VehicleFeature 类是一个独立的类

@JsonInclude(JsonInclude.Include.NON_NULL)
public class VehicleFeature {
private String section;
private String subSection;
private String featureName;
private String subSectionId;
private String sectionName;
private String subSectionName;

标签: javagenerics

解决方案


如果是我,我会将您的addFeatures(...)方法简化为:

private <T> T addFeatures(Class<T> clazz, BaseFeatures theseFeatures) {

    T obj = null;
    
    try {
        
        obj = clazz.getDeclaredConstructor(BaseFeatures.class).newInstance(theseFeatures);              
        
    } catch (ReflectiveOperationException roe) {
        
        roe.printStackTrace();
    }
    
    return obj;
}

我会将这两个构造函数添加到BaseFeatures

public abstract class BaseFeatures{

    protected String featureId;
    
    protected String featureKey;
    
    protected String featureCode;

    /*...*/
    
    protected BaseFeatures(String featureId, String featureKey, String featureCode){
        this.featureId = featureId;
        this.featureKey = featureKey;
        this.featureCode = featureCode;
    }
    
    protected BaseFeatures(BaseFeatures features){
        this.featureId = features.featureId ;
        this.featureKey = features.featureKey;
        this.featureCode = features.featureCode;
    }

    /*...*/
}

您可以在此处查看该实现的实际工作方式

public class BigAssFields {

    /* ... */

    static public void main(String ... args){

        BigAssFields bLike = new BigAssFields();
        
        VehicleFeature vehFeatures = new VehicleFeature("what", "the actual", "Feature");
    
        TechSpecs bigTechSpecs =  bLike.addFeatures(TechSpecs.class, vehFeatures);
    
    }

    /* ... */
}

推荐阅读