标注方法中的特定参数为必填。

huangapple go评论73阅读模式
英文:

Indicate certain parameters in method as mandatory

问题

以下是翻译好的内容:

我正在使用Java制作一个SDK,在这个SDK中,向客户端公开的一个典型方法的外观如下所示:

public void createUser(String name, String email, int age, String bio, String address, String phoneNumber) {
    /*
    方法体
    */
}

问题是,只有姓名(name)和电子邮件(email)字段是强制性的(不能为空或为空),其余字段仅为可选(允许为null值)。除了重写方法并在方法内部抛出运行时异常之外,是否有任何方法可以正确地向调用SDK函数的客户端指示姓名和电子邮件字段是强制性的?

英文:

I am making a SDK using java where a typical method exposed to client would look like this

public void createUser(String name, String email ,int age, String bio, String address, String phoneNumber){
/* 
body
*/
}

So the problem is that only name and email fields are mandatory (not null or empty) and the rest are just optional (null values are allowed). Other than overriding and throwing a run time exception inside the method, Is there any way to properly indicate to the client calling the sdk function that the name and email fields are mandatory ?

答案1

得分: 1

一个广泛使用的库是Project Lombok,并提供了注解@NonNull。你的方法将如下所示。

public void createUser(@NonNull String name, @NonNull String email, int age, String bio, String address, String phoneNumber) {
/* 
body
*/
}

如果使用null值调用该方法的这些字段,将会出现编译错误。

FYI: 针对你的情况,我建议考虑使用构建者设计模式,因为并非所有参数都是必需的。这将使你的“SDK”更加易用。

英文:

A widely used library for that is Project Lombok and the provided annotation @NonNull. Your method would look as follows.

public void createUser(@NonNull String name, @NonNull String email ,int age, String bio, String address, String phoneNumber){
/* 
body
*/
}

If the method is called with null values for those fields a compilation error will occur.

FYI: For your case I would consider using the Builder Design Pattern as not all of your parameters are mandatory. This will make your "SDK" nicer to use.

答案2

得分: 1

Java不幸不支持可选参数解决这个问题的一个方法是使用构建器模式Builder Pattern)。您创建一个单独的`UserBuilder`在其构造函数中只接受必需的参数其他参数通过设置函数传递这还允许您为一些参数设置默认值当所有参数都设置好后调用`UserBuilder.getUser()`,您的`User`对象就会被创建出来通常还要确保`User`构造函数在其他地方不可用

```java
class User{

    String name, email, bio, address, phoneNumber;
    int age;

    User(String name, String email, int age, String bio, String address, String phoneNumber){
        this.name = name;
        this.email = email;
        this.age = age;
        this.bio = bio;
        this.address = address;
        this.phoneNumber = phoneNumber;
    }
}
class UserBuilder{
    String name, email, bio, address, phoneNumber;
    int age;
    public UserBuilder(String name, String email){
        this.name = name;
        this.email = email;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User getUser(){
        return new User(name, email,age,bio,address,phoneNumber);
    }
}

例如,如果您只想设置姓名、年龄和电子邮件,可以像这样使用构建器类创建一个User对象:

UserBuilder builder = new UserBuilder(name,email);
builder.setAge(38);
builder.getUser();
英文:

Java unfortunately doesn't support optional parameters. One solution to this might be to use a Builder Pattern. You create a separate UserBuilder class, which only takes the required arguments in its constructor. Other arguments are passed through set functions. This also allows you to give some of the parameters default values. When all the parameters are set, you call UserBuilder.getUser() and your User object is created for you. Typically you also make sure the User constructor is not available elsewhere.

class User{

    String name, email, bio, address, phoneNumber;
    int age;

    User(String name, String email, int age, String bio, String address, String phoneNumber){
        this.name = name;
        this.email = email;
        this.age = age;
        this.bio = bio;
        this.address = address;
        this.phoneNumber = phoneNumber;
    }
}
class UserBuilder{
    String name, email, bio, address, phoneNumber;
    int age;
    public UserBuilder(String name, String email){
        this.name = name;
        this.email = email;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User getUser(){
        return new User(name, email,age,bio,address,phoneNumber);
    }
}

For example, this is how you would create a User object with the builder class if you only wanted to set name, age, and email:

UserBuilder builder = new UserBuilder(name,email);
builder.setAge(38);
builder.getUser();

答案3

得分: 0

你可以在姓名和电子邮件ID之前添加 @NonNull 注解。

@NonNull - 编译器可以确定在不必调试空指针异常的情况下,代码路径可能会接收到空值的情况。

英文:

You can add @NonNull annotation before name and e-mail id.

@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException.

huangapple
  • 本文由 发表于 2020年4月7日 15:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61075165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定