简体   繁体   English

Java中的每个对象都有自己的构造函数吗?

[英]Does every Object in java have its own Constructor?

This question is because of this line i read A new instance of an object is created by calling a constructor method. 这个问题是因为我读了这一行,所以通过调用构造函数方法来创建对象的新实例。 i agrre but constructor method of what..? 我很讨厌但是什么的构造方法..? an OObject or a Class itself..?.. Sorry if its an amateur question but i'm still learning java and i'm finding it hard to understand. 一个OObject或一个类本身..?..对不起,如果它是一个业余问题,但我仍在学习Java,我发现它很难理解。

and this reminded me of which is first chicken or egg..? 这让我想起了第一个是鸡肉还是鸡蛋。

EDIT: 编辑:

May be my question was not clear, i know how objects are created and every class has a constructor but what i want to know is, every Objects in the heap have their own copy of instance variable. 可能是我的问题不清楚,我知道如何创建对象,每个类都有一个构造函数,但我想知道的是,堆中的每个对象都有自己的实例变量副本。 in the same way will they also have a Constructor with them or its just something that only classes have. 以同样的方式,他们也将拥有一个构造函数,或者只有类才具有的构造函数。

In Object-oriented design, a constructor is what creates an object out of your class definition... 在面向对象的设计中,构造函数是根据类定义创建对象的...

2 key concepts here 这里有2个关键概念

  • Class - a class is the blueprint for what an instantiated object should contain, both behaviour (methods) and information (properties). -类是实例化对象应包含的行为(方法)和信息(属性)的蓝图。 Usually contains a Constructor. 通常包含一个构造函数。

  • Object - The thing that is Created by the constructor, a instatiated version of the Class in practical use. 对象 -由构造函数创建的事物,是实际使用中的Class实例化版本。

Example of a Constructor in use 使用中的构造函数示例

public class MyClass {

    public int intProperty;

    // This is the Constructor, Notice it shares a name with the Class
    public MyClass(int value) {
        intProperty = value;
    }
}

Now to Use the class 现在使用课程

//                            |----------This is calling the constructor
//                            |          and placing a new MyClass object
//                            v          in myClassObejct
MyClass myClassObject = new MyClass(3);

myClassObject.intProperty; // 3

This creates a new MyClass Object 这将创建一个新的MyClass 对象

Java does not work without Classes and Constructors, it is core to the design pattern of the language... 没有类和构造函数,Java 就无法工作, 它是该语言设计模式的核心...

Only Classes have constructors, Objects are the product of Constructors , an Object itself does not contain a constructor. 只有类有构造, 对象构造的产品,对象本身并不包含一个构造函数。

Yes, you call the constructor of the class: 是的,您可以调用该类的构造函数:

MyClass instance = new MyClass();

but note that some objects have special constructors allowed by the language, for example String: 但请注意,某些对象具有该语言允许的特殊构造函数,例如String:

String x = "foobar";

Is very similar to (but not exactly the same as): 与(但不完全相同)非常相似:

String x = new String("foobar");

Note that if no constructors are defined for the class, a default (no-args) constructor is implied . 请注意,如果没有为该类定义任何构造函数,则意味着默认的(no-args)构造函数。

Yes every class has a constructor. 是的,每个类都有一个构造函数。

If you do not explicitly define one, Java will create a default empty one for you. 如果您未明确定义一个,Java将为您创建一个默认的空之一。

In Java every class has one or more constructors, and when you create an object using the new keyword, a constructor of that class is called. 在Java中,每个类都有一个或多个构造函数,当您使用new关键字创建对象时,将调用该类的构造函数。

example: 例:

Integer i = new Integer("1");
//i is the object, and the Integer class constructor gets called

Every class must have its own constructor. 每个类都必须具有自己的构造函数。 Either you provide one or more ctor's or the compiler generates a default no-argument ctor for you. 您提供一个或多个ctor,或者编译器为您生成一个默认的无参数ctor。 If you have your own ctor, than the compiler doesn't generate anything. 如果您有自己的ctor,则编译器不会生成任何东西。

Every object in Java must be created through some Class's constructor, with the exception of a few primitive classes like String, which has special allocation rules. Java中的每个对象都必须通过某些Class的构造函数来创建,除了一些基本类(如String)外,它具有特殊的分配规则。 Even at the most basic level, you can always call 即使是最基本的水平,您也可以随时致电

Object o = new Object();

and since all objects in Java inherit from the Object superclass, most objects will inherit the default constructor. 并且由于Java中的所有对象都继承自Object超类,因此大多数对象将继承默认构造函数。

The exception to this is when a class only has a private constructor--one that cannot be called by any outside classes. 例外是当类仅具有私有构造函数时,任何外部类都不能调用该私有构造函数。 In that case, because the default constructor is no longer necessary, it will not be accessible either. 在这种情况下,由于不再需要默认构造函数,因此也无法访问它。

class MyClass
{
    private MyClass()
    {
        //cannot be called by outside classes
    }
}

In general, this is used either for purely static classes (which don't need instances) or Singleton objects (that want to restrict instantiation). 通常,这用于纯静态类(不需要实例)或Singleton对象(想要限制实例化)。 Therefore all objects are created from some constructor, but not all classes necessarily have a usable constructor. 因此,所有对象都是从某个构造函数创建的,但并非所有类都必须具有可用的构造函数。

The statement "A new instance of an object is created by calling a constructor method" is an incorrect statement and is what's causing the confusion. 语句“通过调用构造函数方法创建对象的新实例”是不正确的语句,并且是造成混淆的原因。 There's no such thing as an instance of an object. 没有像对象实例这样的东西。 An object is an instance of a class. 对象是类的实例。 The following 2 statements are valid: 以下2条语句有效:

An object is created by calling a constructor method. 通过调用构造方法创建对象。

An instance of a class (ie an object) is created by calling a constructor method. 类(即对象)的实例是通过调用构造函数方法创建的。

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

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