如何制作构造函数,如果有 2 个参数,则接受 2;如果有 3 个参数,则接受 3

发布于 2024-11-25 10:42:23 字数 243 浏览 1 评论 0原文

Java 新手...

我有一个名称类,其中包含:

private String firstName;
private String middleInitial;
private String lastName;

作为其实例变量。

如果我的某些数据只有firstName和lastName,没有middleInitial,我将如何创建构造函数,使其只需要2个参数而不是3个?

New to Java...

I have a name class that has:

private String firstName;
private String middleInitial;
private String lastName;

as its instance variables.

If I had certain data that had only firstName and lastName, no middleInitial, how would I make the constructor so that it took only 2 parameters instead of three?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

青柠芒果 2024-12-02 10:42:23

您只需编写一个具有两个参数的构造函数和一个具有三个

public YourClass(String firstName, String lastName) {
   ...
}

public YourClass(String firstName, String middleInitial, String lastName) {
    ...
}

调用者的构造函数,然后调用者就可以根据自己的需要选择使用适当的构造函数。

You simply write a constructor with two parameters and a constructor with three

public YourClass(String firstName, String lastName) {
   ...
}

public YourClass(String firstName, String middleInitial, String lastName) {
    ...
}

Callers can then choose to use the appropriate constructor based on their needs.

被你宠の有点坏 2024-12-02 10:42:23

好吧,有两种选择:

  • 只需拥有一个带有三个参数的构造函数,并使用 null 或 middleInitial 的空字符串来调用它。
  • 重载构造函数,可能会调用另一个构造函数。

作为后者的示例,使用空字符串作为默认的中间名首字母:

public Person(String firstName, String middleInitial, String lastName)
{
    this.firstName = firstName;
    this.middleInitial = middleInitial;
    this.lastName = lastName;
}


public Person(String firstName, String lastName)
{
    this(firstName, "", lastName);
}

但是,编译器需要知道您正在从调用站点调用哪一个。所以你可以这样做:

new Person("Jon", "L", "Skeet");

new Person("Jon", "Skeet");

...但你不能这样做:

// Invalid
new Person(firstName, gotMiddleInitial ? middleInitial : ???, lastName);

并期望编译器决定使用“两个名称”变体。

Well, two options:

  • Just have a constructor with three parameters, and call it using null or the empty string for middleInitial
  • Overload the constructors, possibly calling one from the other.

As an example for the latter, using an empty string as the default middle initial:

public Person(String firstName, String middleInitial, String lastName)
{
    this.firstName = firstName;
    this.middleInitial = middleInitial;
    this.lastName = lastName;
}


public Person(String firstName, String lastName)
{
    this(firstName, "", lastName);
}

However, the compiler will need to know which one you're calling from the call site. So you can do:

new Person("Jon", "L", "Skeet");

or

new Person("Jon", "Skeet");

... but you can't do:

// Invalid
new Person(firstName, gotMiddleInitial ? middleInitial : ???, lastName);

and expect the compiler to decide to use the "two name" variant instead.

强辩 2024-12-02 10:42:23

在 Java 中,构造函数不能有默认参数。这里唯一的选择是编写两个构造函数。幸运的是,Java 允许您从其他构造函数调用构造函数。你可以这样做:

public class MyClass {
    private String firstName;
    private String middleInitial;
    private String lastName;

    public MyClass(String firstName, String middleInitial, String lastName) {
        this.firstName = firstName;
        this.middleInitial = middleInitial;
        this.lastName = lastName;
    }

    public MyClass(String firstName, String lastName) {
        this(firstName, "", lastName);
    }

    ...
}

In Java, constructors can't have default arguments. Your only option here is to write two constructors. Fortunately, Java does allow you to call constructors from other constructors. You could do something like:

public class MyClass {
    private String firstName;
    private String middleInitial;
    private String lastName;

    public MyClass(String firstName, String middleInitial, String lastName) {
        this.firstName = firstName;
        this.middleInitial = middleInitial;
        this.lastName = lastName;
    }

    public MyClass(String firstName, String lastName) {
        this(firstName, "", lastName);
    }

    ...
}
爱的故事 2024-12-02 10:42:23
public Class Name{
private String first;
private String middle;
private String last;

public Name(String first, String middle, String last){
this.first = first;
this.middle = middle;
this.last = last;
}

public Name(String first, String last){
this.first = first;
this.last = last;
}

}
public Class Name{
private String first;
private String middle;
private String last;

public Name(String first, String middle, String last){
this.first = first;
this.middle = middle;
this.last = last;
}

public Name(String first, String last){
this.first = first;
this.last = last;
}

}
余生再见 2024-12-02 10:42:23

您可以使用两个构造函数:

public Person(String firstName, String lastName)
{
    this(firstName, null, lastName);
}

public Person(String firstName, String middleInitial, String lastName)
{
    this.firstName     = firstName;
    this.middleInitial = middleInitial;
    this.lastName =    = lastName;
}

You could use two constructors:

public Person(String firstName, String lastName)
{
    this(firstName, null, lastName);
}

public Person(String firstName, String middleInitial, String lastName)
{
    this.firstName     = firstName;
    this.middleInitial = middleInitial;
    this.lastName =    = lastName;
}
ㄟ。诗瑗 2024-12-02 10:42:23

定义 2 个构造函数,一个有 2 个参数,一个有 3 个参数。

Define 2 constructors, one with 2 parameters and one with 3 parameters.

听闻余生 2024-12-02 10:42:23

您可以编写两个构造函数。

public Person( String firstName, String lastName, String middleName ) { ... }

public Person( String firstName, String lastName ) { ... }

You can write two constructors.

public Person( String firstName, String lastName, String middleName ) { ... }

public Person( String firstName, String lastName ) { ... }
逆光下的微笑 2024-12-02 10:42:23
public void myBike(String name, String color)
{
      System.out.println("My bike's name is " + name + " and is " + color + ".");
}

public void myBike(String name, String color, float height)
{
      System.out.println("My bike's name is " + name + " and is " + color + ".");
      System.out.println("My bike is also " + height + " inches tall.");
}
public void myBike(String name, String color)
{
      System.out.println("My bike's name is " + name + " and is " + color + ".");
}

public void myBike(String name, String color, float height)
{
      System.out.println("My bike's name is " + name + " and is " + color + ".");
      System.out.println("My bike is also " + height + " inches tall.");
}
街道布景 2024-12-02 10:42:23

建造者模式...

class Name {

            Builder builder;


            public String getSurname() {
                return builder.surname;
            }

            // getter...


            public Name(Builder builder) {
                this.builder = builder;
            }


            class Builder {

                String surname = "";
                String middleName = "";
                String name = "";


                Builder surname(String surname) {
                    this.surname = surname;
                    return this;
                }


                Builder middleName(String middleName) {
                    this.middleName = middleName;
                    return this;
                }


                Builder name(String name) {
                    this.name = name;
                    return this;
                }


                Name build() {
                    return new Name(this);
                }

            }
        }

Builder pattern...

class Name {

            Builder builder;


            public String getSurname() {
                return builder.surname;
            }

            // getter...


            public Name(Builder builder) {
                this.builder = builder;
            }


            class Builder {

                String surname = "";
                String middleName = "";
                String name = "";


                Builder surname(String surname) {
                    this.surname = surname;
                    return this;
                }


                Builder middleName(String middleName) {
                    this.middleName = middleName;
                    return this;
                }


                Builder name(String name) {
                    this.name = name;
                    return this;
                }


                Name build() {
                    return new Name(this);
                }

            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文