简体   繁体   English

我可以在java中的公共类中调用参数化构造函数中的默认构造函数吗?

[英]Can I call default constructor from parameterized constructor inside public class in java?

I want to call default constructor from a parameterized constructor inside a public java class. 我想从公共java类中的参数化构造函数调用默认构造函数。

Can I achieve it? 我能实现吗?

Use this(); 使用this(); in the first line of the parametrized constructor and it will call your default constructor. 在参数化构造函数的第一行中,它将调用您的默认构造函数。 Make sure you have default constructor as compiler will not provide one if you declare a parametrized constructor. 确保你有默认的构造函数,因为如果你声明一个参数化的构造函数,编译器就不会提供一个构造函数。

For Java: You might mean the constructor without parameters. 对于Java:您可能意味着没有参数的构造函数。 If so you can use the following code: 如果是这样,您可以使用以下代码:

public class MyClass {
   // no params constructor 
   public MyClass() {
      ...
   }

   // parametrized constructor
   public MyClass(int p1, String p2) {
       this();
   }
}

Hope this helps 希望这可以帮助

yes you can 是的你可以

public YourClass() {
    public YourClass() { super();}
    public YourClass(int x) { this();}
}

provided you have the same argument constructor. 如果你有相同的参数构造函数。 This won't work 这不行

public YourClass() {
    public YourClass(int x, int y) { this(); } // compiler error here
    public YourClass(int x) { super(); }
}

Note: super() calls the super constructor (in this case, class Object, because MyClass extends Object implicitly and class Object has a no arg constructor) that matches the same number of arguments. 注意: super()调用超级构造函数(在本例中为类Object,因为MyClass隐式扩展Object而类Object具有无arg构造函数),它匹配相同数量的参数。

this() calls the constructor of the current class that matches the same number of arguments. this()调用当前类的构造函数,该构造函数匹配相同数量的参数。

In Java, the default constructor is the no-argument constructor that's implicitly provided by the compiler. 在Java中,默认构造函数是编译器隐式提供的无参数构造函数。 And the compiler won't provide one in case you introduce any constructor with arguments. 如果您引入任何带参数的构造函数,编译器将不会提供一个。

In that case you have to explicitly define a no-argument constructor (which is not default by the way, because it's not provided by the compiler), eg public MyClass() { } . 在这种情况下,您必须显式定义无参数构造函数(顺便说一下,它不是默认构造函数,因为它不是由编译器提供的),例如public MyClass() { }

And you can call it from other constructor as this(); 你可以从其他构造函数中调用它作为this(); , which must be the first statement in the constructor where it's being called. ,它必须是构造函数中调用它的第一个语句。

You can just call default constructor with new operator (like this: new Test();) or this();. 您可以使用new运算符调用默认构造函数(如下所示:new Test();)或this();. just Test() is forbidden because its not a method of class. 只是Test()是禁止的,因为它不是类的方法。

package org.gpowork.test;

public class Test {
    private String field;
    private Long time = 0L; 
    public Test(){
        this.time = System.currentTimeMillis();
        System.out.println("Default constructor. "+this.time);
    }
    public Test(String field){
            this();
        Test instance = new Test();
        this.field = field;
    }
    public static void main(String[] args){
        System.out.println("start...");
        Test t1 = new Test();
        System.out.println("-------");
        Test t2 = new Test("field1");
    }
}

You can't call a default constructor once you've created a constructor that takes arguments. 一旦创建了带参数的构造函数,就无法调用默认构造函数。 You'll have to create the no argument constructor yourself in order to make a call from the parameterized constructor. 您必须自己创建无参数构造函数,以便从参数化构造函数进行调用。

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

相关问题 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 如何从默认构造函数调用参数化构造函数? - how to call parameterized constructor from default constructor? 如何从java中的另一个类调用参数化构造函数 - How to call parameterized constructor from another class in java 从参数化构造函数调用默认构造函数的任何方法? - Any way to call the default constructor from a parameterized constructor? 可以在类的构造函数中使用“new”来调用Java中的另一个构造函数吗? - Can “new” be used inside the constructor of the class to call another constructor in Java? 调用类的参数化/默认构造函数? - Invoking parameterized/default constructor of a class? Java使用默认构造函数继承公共类 - Java inheriting public class with default constructor 创建对象后,是否可以看到默认的构造函数调用? 在java中 - Can I see default constructor call Once object is creating | in java 不想从子类调用默认构造函数 - NOT wanting to call the default constructor from child class 什么时候必须在Java中使用默认构造函数和参数化构造函数? - When is mandatory to have a default constructor along with parameterized constructor in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM