理解Java中的泛型和反射使数组通用

时间:2017-12-04 14:35:29

标签: java arrays generics reflection

我正在尝试用Java程序建模火车。

目前我有四个班级:

  • Train
  • BoxCar
  • Cargo
  • Person

BoxCar类的数组为Person,数组为CargoBoxCar只能包含一种类型,因此当从构造函数中提供该类型时,

我决定使用哪个数组(人员数组或货物数组)。问题是,我想在BoxCar“泛型”中创建该数组,以便如果用户想要使用另一种“类型”,则可以轻松设置。 他们不需要在BoxCar类中为该类型声明另一个数组,并更改其余的代码。

我该怎么做?我查看了Generic和Reflection,但我看到的例子并不完全适用于我的场景,而且我是第一次学习这个。 有人可以找出正确的方法吗?

棚车:

class BoxCar {
    String boxType;
    int boxID;
    int count;
    boolean isEmpty;
    Cargo cargo[];
    Person people[];
    int capacity;

    BoxCar(String box_type, int ID, int cap) {
        boxType = box_type;
        boxID = ID;
        isEmpty = true;
        count = 0;
        capacity = cap;
    }
}

人:

class Person {
    String government_ID, name;
    int age;

    Person(String gov_ID, String Name, int Age) {
        government_ID = gov_ID;
        name = Name;
        age = Age;
    }
}

货物:

class Cargo {
    String cargo_ID;
    int height, weight;

    Cargo(String c_id, int w, int h) {
        cargo_ID = c_id;
        height = h;
        weight = w;
    }
}

3 个答案:

答案 0 :(得分:0)

我之前误解了你的问题。这应该是:

public class BoxCarContents {
    ...
}

public class Person extends BoxCarContents {
    ...
}

public class Cargo extends BoxCarContents {
    ...
}

public class BoxCar<E extends BoxCarContents> {
    E[] contents;

    public BoxCar(int capacity) {
        contents = new E[n];
    }
    ...
}

//Alternative way
public class BoxCar {
    BoxCarContents[] contents;

    public BoxCar(BoxCarContents[] boxContents) {
        contents = boxContents;
    }
    ...
}

基本上<E extends BoxCarContents>表示E可以是任何扩展BoxCarContents的东西。

答案 1 :(得分:0)

也许这可以帮助你

ifstream myFile;

答案 2 :(得分:0)

实现这一目标的最佳方法是使用泛型。

泛型是泛型编程的工具,它在2004年版本J2SE 5.0中添加到Java编程语言中。它们旨在扩展Java的类型系统,以允许“一种类型或方法在各种类型的对象上运行,同时提供编译时类型安全性”

https://en.wikipedia.org/wiki/Generics_in_Java

通用E允许根据您的需要传递类型。

class BoxCar<E> {
    private int boxID;
    private int count;
    private boolean isEmpty;
    private E target[];
    private int capacity;

    public BoxCar(int ID, int cap) {
        boxID = ID;
        isEmpty = true;
        count = 0;
        capacity = cap;
    }

    public setTarget(E target[]) {
        thid.target = target;
    }
}

另一种方法是使用接口,例如,这可能是一个解决方案:

实现接口允许类对其承诺提供的行为变得更加正式。接口在类和外部世界之间形成契约,并且该合同在构建时由编译器强制执行。如果您的类声称实现了一个接口,那么该接口定义的所有方法必须出现在其源代码中才能成功编译该类。

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

import java.util.HashMap;
import java.util.Map;

public class A {
    public static void main(String[] args) {
        BoxCar<String, String> boxCar_1 = new BoxCar<>(new Person("gov_id", "name", 35), 1, 1);
        BoxCar<String, String> boxCar_2 = new BoxCar<>(new Cargo("c_id", 1, 1), 1, 1);
    }
}

/**
 * This interface declares the
 * methods for box targets.
 *
 * @param <T> for jey
 * @param <E> for value
 */
interface BoxCarTarget<T, E> {
    /**
     * This method returns the data regarding to
     * a specific BoxCar target.
     *
     * @return Map with specific data.
     */
    Map<T, E> getPayload();
}

class BoxCar<T, E> {
    private BoxCarTarget<T, E> target;
    private int boxID;
    private int count;
    private boolean isEmpty;
    private int capacity;

    BoxCar(BoxCarTarget<T, E> target, int ID, int cap) {
        this.target = target;
        boxID = ID;
        isEmpty = true;
        count = 0;
        capacity = cap;

        /*
         * Here you can handle the payload.
         * I think we can use reflection for
         * extracting data.
         */
        Map<T, E> payload = this.target.getPayload();
    }
}

/**
 * Class person
 */
class Person implements BoxCarTarget<String, String> {
    private String government_ID, name;
    private int age;

    Person(String gov_ID, String Name, int Age) {
        government_ID = gov_ID;
        name = Name;
        age = Age;
    }

   /**
    * This method returns the data regarding to
    * a specific BoxCar target.
    *
    * @return Map with specific data.
    */
    @Override
    public Map<String, String> getPayload() {
        Map<String, String> payload = new HashMap<>();
        payload.put("government_ID", government_ID);
        payload.put("name", name);
        payload.put("age", String.valueOf(age));

        return payload;
    }
}

/**
 * Class Cargo
 */
class Cargo implements BoxCarTarget<String, String> {
    private String cargo_ID;
    private int height, weight;

    Cargo(String c_id, int w, int h) {
        cargo_ID = c_id;
        height = h;
        weight = w;
    }

   /**
    * This method returns the data regarding to
    * a specific BoxCar target.
    *
    * @return Map with specific data.
    */    
    @Override
    public Map<String, String> getPayload() {
        Map<String, String> payload = new HashMap<>();
        payload.put("cargo_ID", String.valueOf(cargo_ID));
        payload.put("height", String.valueOf(height));
        payload.put("weight", String.valueOf(weight));

        return payload;
    }
}

希望这有帮助!