Java 通用的 getter 和 setter 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24859700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:15:24  来源:igfitidea点击:

Generic getter and setter methods

javagenericssettergettergetter-setter

提问by user3859651

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.

我正在尝试编写一个抽象类。这个类将是一个字段。将有不同类型的字段需要扩展字段类并编写自己的 setter 类。例如......会有一个字符串字段和一个整数字段。

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

字符串字段需要扩展Field类,但必须有它自己setValuegetValue它有一个字符串变量来设置和返回方法。数字字段必须与整数相同。

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.

我认为最好的方法是在超类中设置一个通用类型的值,但我不知道该怎么做。非常感谢任何帮助。

回答by dognose

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.

你已经完成了。

回答by schlagi123

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>{}

回答by Jean Logeart

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).

您可能想要添加诸如Field(T value)and 之类的构造函数StringField(String s)

回答by Daniel Figueroa

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 >.

基本上,您可以将泛型视为在运行时告诉您的对象将具有哪种类型的一种方式,这是在<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 Fieldclass generic.

因此,ArrayList<String>将包含类型的ArrayList String。所以回到你不想做的事情是让你的Field类通用。

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,您只需编写:

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:

要编写与运行时确定的类型具有相同类型的通用 getter,您必须执行以下操作:

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 Tis a placeholder for the type that Fieldgets during runtime.

请记住,在这种情况下,它TField在运行时获取的类型的占位符。

回答by jayesh

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 linklink2

更多详情请访问此链接link2

回答by Dan Temple

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:

请注意,我特意使这不是抽象的。除非您确实想为特定字段类型实现额外功能,否则无需将其用作超类。所以你可以像这样定义你的 Sting 字段和数字字段:

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:

当然,如果您确实希望在 Field 的类型化版本中实现额外的实现,则将其抽象化并按如下方式定义您的子类:

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