Java中的构造函数重载-何时使用?
我是一个新的程序员,我想知道什么时候使用重载构造函数是最好的做法,以及它与单个主构造函数的区别 简单的回答是:您应该在需要时使用重载 作为一个实际示例,请看一下JLabel API: JLabel有很多构造函数:一个只接受字符串,一个接受字符串和图标,一个只接受图标,一个根本不接受任何参数Java中的构造函数重载-何时使用?,java,Java,我是一个新的程序员,我想知道什么时候使用重载构造函数是最好的做法,以及它与单个主构造函数的区别 简单的回答是:您应该在需要时使用重载 作为一个实际示例,请看一下JLabel API: JLabel有很多构造函数:一个只接受字符串,一个接受字符串和图标,一个只接受图标,一个根本不接受任何参数 当您想要构造这种类型的JLabel时,您可以使用每个构造函数:一个显示字符串,一个显示字符串和图标,一个只显示图标,或者一个还没有显示任何内容(直到您调用其setter函数之一)。简单的回答是:您应该在需要时
当您想要构造这种类型的JLabel时,您可以使用每个构造函数:一个显示字符串,一个显示字符串和图标,一个只显示图标,或者一个还没有显示任何内容(直到您调用其setter函数之一)。简单的回答是:您应该在需要时使用重载 作为一个实际示例,请看一下JLabel API: JLabel有很多构造函数:一个只接受字符串,一个接受字符串和图标,一个只接受图标,一个根本不接受任何参数
当您想要构造这种类型的JLabel时,您可以使用每个构造函数:一个显示字符串,一个显示字符串和图标,一个只显示图标,或者一个还没有显示任何内容(直到您调用其setter函数之一)。简单的回答是:您应该在需要时使用重载 作为一个实际示例,请看一下JLabel API: JLabel有很多构造函数:一个只接受字符串,一个接受字符串和图标,一个只接受图标,一个根本不接受任何参数
当您想要构造这种类型的JLabel时,您可以使用每个构造函数:一个显示字符串,一个显示字符串和图标,一个只显示图标,或者一个还没有显示任何内容(直到您调用其setter函数之一)。简单的回答是:您应该在需要时使用重载 作为一个实际示例,请看一下JLabel API: JLabel有很多构造函数:一个只接受字符串,一个接受字符串和图标,一个只接受图标,一个根本不接受任何参数
当您想要构造这种类型的JLabel时,可以使用每个构造函数:一个显示字符串,一个显示字符串和图标,一个只显示图标,或者一个不显示任何内容(直到调用其setter函数之一)当您希望允许用户以多种不同方式创建对象时,构造函数重载非常有用。例如,为了能够以以下不同方式创建简单的学生类对象:
new Student(45); // given student id
new Student("name"); // given student name
new Student(45,"name"); // given student registration id and name
这有助于简化根据我们的需求创建对象的任务。您可以将这个概念与各种JavaAPI联系起来,因为它们提供了许多不同的方法来初始化类的对象
您还可以将构造函数重载与构造函数链接相结合。
以下是一个例子:
public Student (int id){
this(id,"ANY-DEFAULT-NAME"); // calls the constructor of same class with 2 params
}
public Student (String name){
this(ANY-DEFAULT-ID,name);// calls the constructor of same class with 2 params
}
public Student (int id,String name){
// here you can initialize the instance variables of the class.
}
当您希望允许用户以多种不同方式创建对象时,构造函数重载非常有用。例如,为了能够以以下不同方式创建简单的学生类对象:
new Student(45); // given student id
new Student("name"); // given student name
new Student(45,"name"); // given student registration id and name
这有助于简化根据我们的需求创建对象的任务。您可以将这个概念与各种JavaAPI联系起来,因为它们提供了许多不同的方法来初始化类的对象
您还可以将构造函数重载与构造函数链接相结合。
以下是一个例子:
public Student (int id){
this(id,"ANY-DEFAULT-NAME"); // calls the constructor of same class with 2 params
}
public Student (String name){
this(ANY-DEFAULT-ID,name);// calls the constructor of same class with 2 params
}
public Student (int id,String name){
// here you can initialize the instance variables of the class.
}
当您希望允许用户以多种不同方式创建对象时,构造函数重载非常有用。例如,为了能够以以下不同方式创建简单的学生类对象:
new Student(45); // given student id
new Student("name"); // given student name
new Student(45,"name"); // given student registration id and name
这有助于简化根据我们的需求创建对象的任务。您可以将这个概念与各种JavaAPI联系起来,因为它们提供了许多不同的方法来初始化类的对象
您还可以将构造函数重载与构造函数链接相结合。
以下是一个例子:
public Student (int id){
this(id,"ANY-DEFAULT-NAME"); // calls the constructor of same class with 2 params
}
public Student (String name){
this(ANY-DEFAULT-ID,name);// calls the constructor of same class with 2 params
}
public Student (int id,String name){
// here you can initialize the instance variables of the class.
}
当您希望允许用户以多种不同方式创建对象时,构造函数重载非常有用。例如,为了能够以以下不同方式创建简单的学生类对象:
new Student(45); // given student id
new Student("name"); // given student name
new Student(45,"name"); // given student registration id and name
这有助于简化根据我们的需求创建对象的任务。您可以将这个概念与各种JavaAPI联系起来,因为它们提供了许多不同的方法来初始化类的对象
您还可以将构造函数重载与构造函数链接相结合。
以下是一个例子:
public Student (int id){
this(id,"ANY-DEFAULT-NAME"); // calls the constructor of same class with 2 params
}
public Student (String name){
this(ANY-DEFAULT-ID,name);// calls the constructor of same class with 2 params
}
public Student (int id,String name){
// here you can initialize the instance variables of the class.
}
您可以根据需要重载构造函数。例如,假设您有一个名为Dog的简单类,该类具有一些属性,如:名称、品种、生日、所有者和肤色
public class Dog {
private String name;
private String breed;
private Date birthday;
private String owner;
private String skinColor;
/*Getters and Setters*/
...
}
如果您实例一个Dog类型的对象,并且想要设置一个全部或部分属性值,那么您必须调用该对象的所有setters方法,但是使用构造函数,您可以在每次实例对象时直接通过值保存该步骤
例如:
public class Dog {
private String name;
private String breed;
private Date birthday;
private String owner;
private String skinColor;
public Dog(String name, String breed,Date birthday,String owner,String skinColor){
this.name = name;
this.breed = breed;
this.birthday = birthday;
this.owner = owner;
this.skinColor = skinColor;
}
/*Getters and Setters*/
...
}
Dog myDog = new Dog("Jose", "Chiguagua",new Date(),"Jhon","Brown");
如果只想用名称来实例对象,也可以这样做。一个好的实践是,如果您有一个具有填充某个点所必需的属性的对象,请提供默认构造函数,如果您不提供它,则始终需要传递一些值,例如对象。这为程序员提供了灵活性。您可以根据需要重载构造函数。例如,假设您有一个名为Dog的简单类,该类具有一些属性,如:名称、品种、生日、所有者和肤色
public class Dog {
private String name;
private String breed;
private Date birthday;
private String owner;
private String skinColor;
/*Getters and Setters*/
...
}
如果您实例一个Dog类型的对象,并且想要设置一个全部或部分属性值,那么您必须调用该对象的所有setters方法,但是使用构造函数,您可以在每次实例对象时直接通过值保存该步骤
例如:<