首頁  >  文章  >  如何製作保存物件的 getter/setter 的 Map 的靜態版本?

如何製作保存物件的 getter/setter 的 Map 的靜態版本?

PHPz
PHPz轉載
2024-02-06 08:03:03598瀏覽
問題內容

我有一個在呼叫物件時建立的映射,該映射為呼叫者提供變數名稱作為鍵和 getter/setter 對作為值。這按預期工作。我的問題是,我每次調用它時都會構建它,我想知道是否有一種方法可以將其聲明為靜態並僅提供我想要調用它的對象,這樣我就不會每次都構建地圖因為getter 和setter 在運作時不會改變。

我有:

package main;
import org.javatuples.Pair;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

public interface MapPackHelperI 
{
     public Map<String, Pair<Supplier, Consumer>> getNameToGetterSetterMap();    
}
package main;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.javatuples.Pair;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class SomeStruct implements MapPackHelperI
{
    private long somelong;
    private String somestring;
    private List<Float> somelistfloat;
    private SomeEnum someenum;

    public  Map<String, Pair<Supplier, Consumer>> getNameToGetterSetterMap()
    {
        Map<String, Pair<Supplier, Consumer>> nameToGetterSetterMap = new HashMap<>();        
        nameToGetterSetterMap.put("somelong", Pair.with( this::getSomelong, (Consumer<Long>)this::setSomelong));
        nameToGetterSetterMap.put("somestring", Pair.with( this::getSomestring, (Consumer<String>)this::setSomestring));
        nameToGetterSetterMap.put("somelistfloat", Pair.with( this::getSomelistfloat, (Consumer<List<Float>>)this::setSomelistfloat));
        nameToGetterSetterMap.put("someenum", Pair.with( this::getSomeenum, (Consumer<SomeEnum>)this::setSomeenum));
        return nameToGetterSetterMap;
    }

    public long getSomelong() {
        return this.somelong;
    }

    public void setSomelong(long somelong) {
        this.somelong = somelong;
    }

    public String getSomestring() {
        return this.somestring;
    }

    public void setSomestring(String somestring) {
        this.somestring = somestring;
    }

    public List<Float> getSomelistfloat() {
        return this.somelistfloat;
    }

    public void setSomelistfloat(List<Float> somelistfloat) {
        this.somelistfloat = somelistfloat;
    }

    public SomeEnum getSomeenum() {
        return this.someenum;
    }

    public void setSomeenum(SomeEnum someenum) {
        this.someenum = someenum;
    }

   // ... hashcode, toString, and equals, not relevant for the example
}

這允許我從其他地方做:

public static String serialize(MapPackHelperI objectToSerialize) 
{
   Map<String, Pair<Supplier, Consumer>> nameToGetterSetterMap = objectToSerialize.getNameToGetterSetterMap();
   for(Entry<String, Pair<Supplier, Consumer>> current : nameToGetterSetterMap.entrySet())
   {
       String name = current.getKey();
       Supplier getter = current.getValue().getValue0();
       //code that serializes into string with name and getter regardless of the MapPackHelperI I pass in
   }

  // return the string representation of the object. I have another method that uses the same map to go the other way with the setter.
}

就像我說的,這可行,但每次調用它時我都會實例化並填充地圖。

有沒有辦法讓SomeStruct 有一個public static Map7ac420dc38be31c2cf1f91a147206a67> nameToGetterSetterMap = new HashMapa8093152e673feb7aba1828c43532094();(或一些等等效的),這樣 public Map05dbde19e2ff5497399c066cd6cfc66a> getNameToGetterSetterMap() 將實例化的參數作為參數SomeStruct 並僅將呼叫套用於該特定物件。

我嘗試做一個靜態地圖並用 SomeStruct::getSomelong 等填充它,但編譯器說我不能這樣做。


正確答案


成員欄位

答案似乎如此明顯,我一定錯過了一些東西:將新構造的映射緩存為私有成員字段。

public class SomeStruct implements MapPackHelperI
{
    private long somelong;
    private String somestring;
    private List<Float> somelistfloat;
    private SomeEnum someenum;
    private Map<String, Pair<Supplier, Consumer>> map = 
        Map.of( 
            "somelong", Pair.with( this::getSomelong, (Consumer<Long>)this::setSomelong) ,
            "somestring", Pair.with( this::getSomestring, (Consumer<String>)this::setSomestring) ,
            "somelistfloat", Pair.with( this::getSomelistfloat, (Consumer<List<Float>>)this::setSomelistfloat) ,
            "someenum", Pair.with( this::getSomeenum, (Consumer<SomeEnum>)this::setSomeenum)
        ) ;

    public  Map<String, Pair<Supplier, Consumer>> getNameToGetterSetterMap()
    {
        return this.map ;
    }

…

請注意 Map.of不可修改

您應該能夠將map 成員變數設為static,但除非您確定自己正在對SomeStruct 進行無數次實例化,否則我認為這是不值得麻煩的微優化。

以上是如何製作保存物件的 getter/setter 的 Map 的靜態版本?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除