简体   繁体   English

如何从java中的另一个类调用参数化构造函数

[英]How to call parameterized constructor from another class in java

I have a class in Java that looks like this:我有一个 Java 类,如下所示:

  public class UserUrl{
    
        public final MongoUserId mongoUserId;
        public final Optional<String> url;
        public final long version;
        public final Instant requestedDate;
        public final Instant createdDate;
        public final State state;

public UserUrl(MongoUserId mongoUserId,
                   Optional<String> url,
                   long version,
                   Instant requestedDate,
                   Instant createdDate, State state) {
        this.mongoUserId = mongoUserId;
        this.url = url;
        this.version = version;
        this.requestedDate = requestedDate;
        this.createdDate = createdDate;
        this.state = state;
    }
    
        public UserUrl withUrl(String url){
            return new UserUrl(
                    this.mongoUserId,
                    Optional.of(url),
                    1,
                    this.requestedDate,
                    Instant.now(),
                    State.COMPLETED
            );
        }
    }

I want to access constructor from another class, and I am trying to do it like this:我想从另一个类访问构造函数,我正在尝试这样做:

 UserUrl userUrl = new UserUrl();
 userUrlRepository.save(userUrl.withUrl(url));

But I am not able because但我不能因为

UserUrl userUrl = new UserUrl();

is looking for parameters.正在寻找参数。

What is good way of doing this?这样做的好方法是什么?

Well, I think you're missing something here.好吧,我认为你在这里遗漏了一些东西。 I there is not a no-arg constructor, you obviously cannot call it, since it doesn't exist.我没有无参数构造函数,你显然不能调用它,因为它不存在。

You have two options here.您在这里有两个选择。

  • Either declare a no-arg constructor:要么声明一个无参数构造函数:

     UserUrl() { // Optionally initialize your properties with default values. }
  • Or use the existing constructor:或者使用现有的构造函数:

     UserUrl userUrl = new UserUrl(null, Optional.of(url), 1, null, Instant.now(), State.COMPLETED); userUrlRepository.save(userUrl); // No need to set the URL

暂无
暂无

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

相关问题 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 如何从默认构造函数调用参数化构造函数? - how to call parameterized constructor from default constructor? 我可以在java中的公共类中调用参数化构造函数中的默认构造函数吗? - Can I call default constructor from parameterized constructor inside public class in java? 如何在使用 class 参数化的 java 中创建构造函数? - How to create constructor in java parameterized with class? 如何从没有构造函数的另一个类调用方法 - How to call a method from another class with no constructor 如何在JAVA中使用参数化构造函数制作一个类的长度不确定的对象数组? - How to make an array of objects of undefined length of a class with parameterized constructor in JAVA? 如何正确地从另一个类调用构造函数,然后将其打印为数组以进行控制台? - How to call constructor from another class correctly and then print it as an array to console? 如何从另一个类构造函数调用变量(长度,宽度,高度) - How to call the variables (length,breadth,height) from another class constructor 如何在 Java 中从另一个构造函数调用一个构造函数? - How do I call one constructor from another in Java? 如何从另一个模块调用Java类 - How to call a class of java from another module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM