有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用“this”关键字调用类中具有1个以上参数的多个构造?

我在研究这个()关键字来调用构造函数。我非常确定其用于调用当前类的其他构造函数的简单机制:

public class AsCaller {
String name;
int id;

AsCaller() {
    System.out.println("No arguments");
}
AsCaller(String n, int i) {
    this();
    name = n;
    id = i;
}

void display() {
    System.out.println(id + " " +name);
}

public static void main(String[] args) {
    AsCaller example1 = new AsCaller("Student", 876);       
    example1.display();
  }
}

正如预期的那样,它提供了以下输出:

No arguments
876 Student

但是,如果我们有两个以上的构造函数,其中一些有两个或更多的参数,会怎么样呢。如何使用this()关键字调用其中一个?比如:

public class AsCaller {
String name;
int id;

AsCaller(String city, String street, float house) {
    System.out.println("float datatype invoked");
}

AsCaller(int age, int marks) {
    System.out.println("int datatype invoked");
}

AsCaller(String n, int i) {
    // How to use this() keyword to invoke any of the above constructor?.
    this();
    name = n;
    id = i;
}

    AsCaller() {
    System.out.println("No arguments");
   }
void display() {
    System.out.println(id + " " +name);
}

public static void main(String[] args) {
    AsCaller example1 = new AsCaller("Student", 876);

    example1.display();
    }
}

共 (3) 个答案

  1. # 1 楼答案

    您可以这样做:

    AsCaller(String n, int i) {
        // How to use this() keyword to call any of the above constructor?.
        this(1,i);// calls the AsCaller(int age, int marks) constructor
        name = n;
        id = i;
    }
    

    以类似的方式,您需要将参数传递给此(..)与要调用的构造函数的参数序列匹配

  2. # 2 楼答案

    我认为从一个构造函数调用两个构造函数是没有意义的。编码该类的编码器选择提供的构造函数类型是设计选择,这样就不需要从一个构造函数调用两个构造函数。 我认为Java允许您从构造函数中调用一个构造函数的原因是,默认构造函数可能会进行一些默认初始化,例如打开套接字、分配内存等,这在构造函数中很常见。 您可以按如下方式重写代码,这样就不必从一个构造函数中调用两个构造函数,尽管功能是保留的

        public class AsCaller {
        String name;
        int id;
    
    
    
        void intinit(int age, int marks){
            System.out.println("int datatype invoked");
        }
        void floatinit(String city, String street, float house){
            System.out.println("float datatype invoked");
        }
        AsCaller(String city, String street, float house) {
            floatinit(city,street,house);
        }
    
        AsCaller(int age, int marks) {
            intinit(age,marks);
        }
    
        AsCaller(String n, int i) {
          // How to use this() keyword to invoke any of the above constructor?.
            this();
            intinit(1,2);
            floatinit("a","b",3.0f);
            name = n;
            id = i;
        }
    
        AsCaller() {
            System.out.println("No arguments");
        }
        void display() {
            System.out.println(id + " " +name);
        }
    
        public static void main(String[] args) {
            AsCaller example1 = new AsCaller("Student", 876);
    
            example1.display();
        }  
        }
    

    TL;DR:不,编译器不允许您从类的任何构造函数调用两个构造函数,因为对该构造函数的调用必须是构造函数中的第一条语句

  3. # 3 楼答案

    简单地说:

     this(age, marks);
    

    传递与要调用的构造函数匹配的参数