简体   繁体   English

采用不同 class 对象的通用方法

[英]Generic method to take different class objects

I have 3 class(techspecs, packages, and features) objects where they all share the same fields.我有 3 个类(技术规范、包和功能)对象,它们都共享相同的字段。 The fields are big and instead of repeating setting the fields of each field 3 times(which ends up looking like duplicates), I would like to pass the class objects into one method that uses the generic object to setting the object fields.这些字段很大,而不是重复设置每个字段的字段 3 次(最终看起来像重复),我想将 class 对象传递给一种使用通用 object 设置 ZA8CFDE6331BD59EB66666ZB4 字段的方法。

I tried passing the class object as a generic but then i dont have access to its members.我尝试将 class object 作为通用传递,但后来我无法访问其成员。 This is what i tried这是我试过的

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

Then然后

    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;

EDIT Each of the 3 classes extend BaseFeatures编辑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;

//getters and setters. //getter 和 setter。

Here is one of the classes.这是其中一堂课。

public class TechSpecs extends BaseFeatures {

private String techSpecs;


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

public String getTechSpecs(){
    return techSpecs;
}

}

All of these fields need to be set in the class object of all 3 classes所有这些字段都需要在所有 3 个类的 class object 中设置

EDIT 2编辑 2

VehicleFeature Class is a standalone class VehicleFeature Class 是独立的 class

@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;

If it were me, I would simplify your addFeatures(...) method to something like:如果是我,我会将您的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;
}

I'd add these two constructors to BaseFeatures :我会将这两个构造函数添加到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;
    }

    /*...*/
}

You can see how that implementation would actually work , here:您可以在此处查看该实现的实际工作方式

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

    /* ... */
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中是否可以定义一个通用方法来将超类型类作为参数? - In Java is it possible to define a generic method to take a supertype class as an argument? Java泛型方法将不同的对象添加到不同的数组列表中 - Java generic method adding different objects into different arraylists 原始类的通用类型可以采用该通用类的所有不同变体这一事实与Java兼容性有关吗? - The fact that a raw type of generic class can take all different variations of that generic class is matter of Java compatibility? 为什么这个泛型java方法接受两个不同类型的对象? - Why does this generic java method accept two objects of different type? Java / Android:通用方法-将相同的功能应用于不同的对象 - Java/Android: generic method - apply same functionality to different objects 传递一个类并返回不同的类作为java泛型方法的返回对象 - pass a class and returning different class as return object of java generic method Java 定义通用 class 作为 static 方法的参数,以传递实体对象 - Java defining generic class as parameter for static method, to pass entity objects Factory方法,用于实例化泛型类中使用的对象的实例 - Factory method to instantiate instance of objects used in a generic class 我们如何将通用 class 的对象传递给 Java 中的方法 - How do we pass objects of generic class to a method in Java 不同对象的通用活动 - Generic activities for different objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM