简体   繁体   English

如何仅使用特定字段创建新的 java object class?

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

I have such java class:我有这样的 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:当我想创建一个新的 class object 我必须这样做:

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.我正在尝试过滤此 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.但例如我想只考虑这个 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;我有一个想法:创建一些最终FirstTypeMD finalConf; class object which will have only important fields and than try to compare objects: class object 只有重要的字段,而不是尝试比较对象:

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:但我收到错误消息说我没有一些字段是 null 像这样:

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在这里我使用了两个构造函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java中只使用某些类对象值? - How to use only certain class object values in java? 如何使用 Java 反射在 Java 中创建嵌套静态类的新对象 - How to create new object of nested static class in Java with Java Reflection 如何使用Java中的for循环从类创建新对象? - How would I create a new object from a class using a for loop in java? 如何在 Object Model class 中创建字符串字段列表 - How to create a List of String field in Object Model class in java? How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? - How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? 如何在Java中创建class LocalDate的object? 为什么不使用 new 关键字? - How to create the object of class LocalDate in Java? And why not use new keyword? Java-如何将对象创建限制为仅使用另一个类创建它 - Java - How to limit the object creation to only create it using another class 如何为特定年龄的java创建验证 - How to create a validation for only certain ages java Java类:我需要向类添加String类型的新字段。 我应该扩展(创建一个新的派生类)还是简单地添加新字段? - Java Class: I need to add a new field of type String to a class. Should I extend (create a new derived class) or simply add the new field? 构造函数是在 JAVA 中创建 class 的 object 的唯一方法吗? - Is constructor the only way to create the object of a class in JAVA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM