mardi 20 juillet 2021

Java inheritence with different typed variables

with the code below is there a way to make these two extremely similar classes inherit from a single superclass? they differ almost exclusively in variable types. If not is there a design pattern that seems most appropriate for this case? I've been looking at maybe using a factory pattern but I'm not sure if that's the most appropriate for the situation.

class stringCharacteristic {
    String name = "";
    String value = "";

    public String getValue() {
        return value;
    }

    public stringCharacteristic setValue(String value) {
        this.value = value;
        return this;
    }

    public String getName() {
        return name;
    }

    public stringCharacteristic setName(String name) {
        this.name = name;
        return this;
    }
}

class intCharacteristic {
    String name = "";
    int value = 0;

    public int getValue() {
        return value;
    }

    public intCharacteristic setValue(int value) {
        this.value = value;
        return this;
    }

    public String getName() {
        return name;
    }

    public intCharacteristic setName(String name) {
        this.name = name;
        return this;
    }
}

Aucun commentaire:

Enregistrer un commentaire