简体   繁体   中英

How I create new java object class with only certain field?

I have such java class:

public class FirstTypeMD extends BaseParams {
    Integer framePerSec;

    public FirstTypeMD(Integer energyCons, Integer mass, Integer tempRange, Boolean electricProtect,
            Boolean radioProtect, Integer framePerSec) {
        super(energyCons, mass, tempRange, electricProtect, radioProtect);

        this.framePerSec = framePerSec;
    }


    public void setFramePerSec(Integer framePerSec) {
        this.framePerSec = framePerSec;
    }

    public Integer getFramePerSec() {
        return framePerSec;
    }

}

when I would like to create a new class object I have to do it in such way:

new  FirstTypeMD(energyCons, mass, tempRange, electricProtect, radioProtect, framePerSec)

But I would like to pass only first parameter for example like this:

new  FirstTypeMD(energyCons)

Maybe my general idea is wrong so I would like to describe what I want to do. I'm trying to filter array of objects of this class. I can do it in such way right now:

ArrayList<FirstTypeMD> resArr = new ArrayList<FirstTypeMD>(resArr1.stream()
                            .filter(c -> c.electricProtect == elProtect.getSelection()
                                    && c.radioProtect == radProtect.getSelection())
                            .collect(Collectors.toList()));

But for example I would like to take into account only one or only two fields of this class. Of course I can add a lot of code and finally solve my problem, but maybe I can do it in better way?
One idea I had: create some final FirstTypeMD finalConf; class object which will have only important fields and than try to compare objects:

ArrayList<FirstTypeMD> resArr = new ArrayList<FirstTypeMD>(resArr1.stream()
                            .filter(c -> c.electricProtect == finalConf.electricProtect).collect(Collectors.toList()));

but I received the error which said that I hadn't have some fields which are null like this:

Cannot invoke "testwizard.wizards.FirstTypeMD.setElectricProtect(java.lang.Boolean)" because "this.this$0.finalConf" is null  

I don't know maybe I would like something impossible but I decided to ask you:)

public class Main {
    FirstTypeMD firstTypeMD = new FirstTypeMD(1);
}

class FirstTypeMD extends BaseParams {
    Integer framePerSec;

    public FirstTypeMD(Integer energyCons, Integer mass, Integer tempRange, Boolean electricProtect,
                       Boolean radioProtect, Integer framePerSec) {
        super(energyCons, mass, tempRange, electricProtect, radioProtect);

        this.framePerSec = framePerSec;
    }

    public FirstTypeMD(Integer energyCons) {
       
    }

    public void setFramePerSec(Integer framePerSec) {
        this.framePerSec = framePerSec;
    }

    public Integer getFramePerSec() {
        return framePerSec;
    }
}

In here I used two constructors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM