简体   繁体   English

Java中如何调用构造函数

[英]How to call Constructors in Java

I have following code in java:我在java中有以下代码:

// SingleMaltView class
class abc {
    public abc(display p) {
        // display is another class.
    }
}

class sample {
    public static void main(String[] args) {
        abc ob = new abc(); // error
    }
}

How to call constructor of abc class?如何调用abc类的构造函数?

Your constructor has an argument / parameter, use it:你的构造函数有一个参数/参数,使用它:

public abc(display p)
//         ↑  here!!!!

This means you must provide a display object to create the abc object, so in the main method you must make something like that:这意味着你必须提供一个display对象来创建abc对象,所以在 main 方法中你必须做这样的事情:

public static void main(String[] args) {
    display d = new display(); // assuming display has no-argument constructor
    abc ob = new abc(d);   // NO error! :)
}

NOTES:注意事项:

  • java objects by convention starts in UPPERCASE , so names of files and classes should be Display and Abc (or ABC ).按照约定,java 对象以UPPERCASE开头,因此文件和类的名称应该是DisplayAbc (或ABC )。
  • if you don't put any code into constrctor, dont create it如果您没有将任何代码放入构造函数中,请不要创建它
  • java provides a default empty constructor to instantiate your objects when you don't create any. java 提供了一个默认的空构造函数来在您不创建任何对象时实例化您的对象。

If you dont provide constructor in a class, compiler inserts by default no arguments constructor but if you have provided then compiler wont insert default constructor.如果您不在类中提供构造函数,编译器默认插入无参数构造函数,但如果您提供了,则编译器不会插入默认构造函数。 Here you are creating an object of a class in which parameterized constructor is present and you are creating object of class ABC with no parameters so compiler is unable to locate no argument constructor and hence gives error below code can solve this riddle.在这里,您正在创建一个存在参数化构造函数的类的对象,并且您正在创建没有参数的 ABC 类的对象,因此编译器无法找到没有参数的构造函数,因此在代码下方给出错误可以解决这个谜语。

    public abc()    // another constructor
    {

    }

or change creation type或更改创建类型

     public static void main(String[] args)
     {
              Display d =new Display();
              abc ob = new abc(d);   // error
     }

Here you are calling default constructor which is not there in your abc class .在这里,您正在调用abc 类中不存在的默认构造函数。 rather you have a single parameterized constructor.相反,您只有一个参数化的构造函数。

public static void main(String[] args) {
    abc ob = new abc();   // error
} 

And here you are creating an object which has no argument.在这里,您正在创建一个没有参数的对象。

So you should pass here something like this,所以你应该像这样通过这里,

display obj = new display();
abc ob = new abc(obj);

We can define a constructor in 2 ways: (i) Default constructor (ii) Parametrised constructor我们可以通过两种方式定义构造函数:(i) 默认构造函数 (ii) 参数化构造函数

(i)Default constructor syntax: ConstructorName() {Statements.. } (i) 默认构造函数语法:ConstructorName() {Statements.. }

(ii)Parametrised constructor syntax: ConstructorName(Parameter/Arguments) {Statements.. } (ii) 参数化构造函数语法:ConstructorName(Parameter/Arguments) {Statements.. }

*Syntax to call a constructor ClassName reference variable = new TypeOfConstructor() * 调用构造函数 ClassName 引用变量的语法 = new TypeOfConstructor()

*To call the variable and method of constructor which defined under the scope, call it by using reference variables. *调用作用域下定义的构造函数的变量和方法,使用引用变量调用。

Note: Constructor doesn't have any return type and access specifier注意:构造函数没有任何返回类型和访问说明符

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

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