简体   繁体   English

Java 构造函数重载

[英]Java Constructor Overloading

I'm new with Java and I'm having trouble understanding the constructor issue, I have looked at many tutorials and still I'm having difficult to understand why we use constructors, anyway, my specific question is :我是 Java 新手,无法理解构造函数问题,我看过很多教程,但仍然难以理解我们为什么使用构造函数,无论如何,我的具体问题是:

Correct me if I'm wrong, if i want to add in my class more than one constructor, I'll write the first one and the second will be int type (inside the brackets).如果我错了,请纠正我,如果我想在我的类中添加多个构造函数,我将编写第一个,第二个将是int类型(在括号内)。

  1. is it because the constructors have to be with the same name as the class and we need to distinguish between them ?是不是因为构造函数必须与类同名,我们需要区分它们?

  2. what if I want to add a third constructor ?如果我想添加第三个构造函数怎么办? Can it also be int type ?它也可以是int类型吗?

a) is it because the constructors have to be with the same name as the class and we need to distinguish between them ? a) 是不是因为构造函数必须与类同名,我们需要区分它们?

Yes constructors are always the name of the class without any return type, and in order to distinguish between them, you can have different parameters.是的,构造函数总是类名,没有任何返回类型,为了区分它们,你可以有不同的参数。

b) what if i want to add a third constructor ? b) 如果我想添加第三个构造函数怎么办? it can be also int type ?它也可以是 int 类型?

Yes, you can add any no.是的,您可以添加任何否。 of overloaded constructors but those all should be different in no.重载的构造函数,但这些都应该有所不同。 and/or type of parameters.和/或参数类型。

Like :-喜欢 :-

public User() // default constructor
{
}

public User(int age) // overloaded constructor with int
{
}

public User(String name) // // overloaded constructor with String
{
}

public User(String name, int age) // // overloaded constructor with String and int
{
}

Yes a constructor has the same name as the Class .是的,构造函数与Class同名。

As long as the constructors have different signatures you can have as many as you want.只要构造函数具有不同的签名,您就可以拥有任意数量的签名。 The signature is what distinguishes one constructor from another...签名是区分一个构造函数与另一个构造函数的地方...

public MyClass()
{
}

public MyClass(int a)
{
}

public MyClass(int a, int b)
{
}

public MyClass(String a)
{
}

public MyClass(int a, String b)
{
}

Those are all different because they have different signatures.这些都是不同的,因为它们有不同的签名。

Actually, if you want to have 10000 constructor, you can as long a signature are differents.实际上,如果您想拥有 10000 个构造函数,只要签名不同就可以。

public class People {
    public People(String name, int age) {
        ...
    }
    public People(String name) {
        ...
    }
}

You can construct your object in a different way.你可以用不同的方式构造你的对象。 You can see an example by yourself looking at : the java String class wich has a plenty of constructors.你可以自己看一个例子: java String 类有很多构造函数。



And yes, all constructors have the same name of his class.是的,所有构造函数都与他的类同名。




But this will not works :但这行不通:

public class People {
    public People(String name, int age) {
        ...
    }
    public People(String name, int numberOfLegs) {
        ...
    }
}

Since you have two constructors with the same signature由于您有两个具有相同签名的构造函数

The constructors purpouse is to contain the code to inizialize the object.构造函数的目的是包含初始化对象的代码。 Usually, the initialization is done using constructor parameters.通常,初始化是使用构造函数参数完成的。 You can have different constructors with different parameters list, as needed by your context.根据上下文的需要,您可以使用具有不同参数列表的不同构造函数。 It is a good pratice to do constructor chaining , that is calling a base constructor from others.进行构造函数链接是一种很好的做法,即从其他人调用基构造函数。

Adding to the @brso05 answer,添加到@brso05 答案中,

This is also one of the ways:这也是其中一种方式:

public MyClass( int a)
{
}

public MyClass( int a, int b)
{
}

public MyClass( int a, String b)
{
}

And So on..It is the arguments which make difference, rest remains the same!依此类推..这是不同的论点,其余保持不变!

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

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