Generic getter and setter methods [closed]

馋奶兔 提交于 2019-12-11 03:11:57

问题


I am trying to write an abstract class. This class is going to be a Field. There will be different types of fields which will need to extend the field class and write its own setter class. For example....There will be a String Field and an Integer Field.

String Field will need to extend the Field class but must have it's own setValue and getValue method which has a String variable to set and return. Number Field must do the same with Integer.

I think the best way is to have a Generic Type set Value in the super class but I am not sure how to do about this. Any help much appreciated.


回答1:


Like this:

public abstract class Field<T> {
    private T value;
    // + constructor(s)
    public T get() {
        return value;
    }
    public void set(T value) {
        this.value = value;
    }
}

public class StringField extends Field<String>{}
public class IntField extends Field<Integer>{}

You might want to add constructors such as Field(T value) and StringField(String s).




回答2:


This looks like a really good practice to get acquainted with generics. Now there are a bunch of tutorials available so I'll try to explain a little bit what is happening and then I'll urge you too look up a few of them.

Basically you can think of generics as a way to tell your object during runtime which type(s) it will have, this is the type within the < and >.

So ArrayList<String> will be an ArrayList containing the type String. So going back to what you wan't to do is make your Field class generic.

This is easily accomplished with:

public class Field<T> {
    private T field;
}

Now when you create an instance of your Field class, say for a String, you'll simply write:

Field<String> f = new Field<>();

To write a generic getter with the same type as the type decided during runtime you will have do the following:

public class Field<T> {
    private T field;

    public T getField() { return field; }
}

I'll leave creating the setter as an excercise for you.

Remember that in this case T is a placeholder for the type that Field gets during runtime.




回答3:


You can make your super class generic:

public abstract class Field<T>{
  private T value;

  public void setValue(T value){
     this.value = value;
  }

  public T getValue(){
     return value;
  }
}

if you now extend it like:

public class StringField extends Field<String>{
   //nothing to do here.
}

you are already done.




回答4:


Basic implementation

public abstract class Field<T> {
    private T value;
    // + constructor(s)
    public T get() {
        return value;
    }
    public void set(T value) {
        this.value = value;
    }
}

public class StringField extends Field<String>{}
public class IntField extends Field<Integer>{}

for more details Please visit this link link2




回答5:


I think what you've suggested is perfectly reasonable, it would look something like this:

public class Field<T extends Object> {

    private T value;

    public Field(final T value) { 
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(final T value) {
        this.value = value;
    }
}

Note, I have made this specifically not abstract on purpose. There's no need to use this as a super class unless you actually want to implement extra functionality for the specific field types. So you could define your Sting Fields and Number Fields like this:

Field<String> stringField = new Field<String>();
Field<Integer> numberField = new Field<Integer>();

Of course, if you do want extra implementations within the typed versions of Field, then make it abstract and define your subclasses as follows:

public class StringField extends Field<String> {
    public StringField(final String value) {
        super(value);
    }
}

public class NumberField extends Field<Integer> {
    public NumberField(final Integer value) {
        super(value);
    }
}



回答6:


public abstract class MyClass<T>{
  private T field;

  public T getField(){
    return field;
  }

  public void setField(T field){
    this.field = field;
  }
}


public class MyClassWithString extends MyClass<String>{}


来源:https://stackoverflow.com/questions/24859700/generic-getter-and-setter-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!