首頁 > Java > 主體

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

PHPz
發布: 2024-02-06 08:03:03
轉載
624 人瀏覽過
問題內容

我有一個在呼叫物件時建立的映射,該映射為呼叫者提供變數名稱作為鍵和 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 Map<String, Pair<Supplier, Consumer>> nameToGetterSetterMap = new HashMap<>();(或一些等等效的),這樣 public Map<String, Pairphpcnltphp cnSupplier、Consumer>> 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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!