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

发布于 2024-12-05 20:00:15 字数 283 浏览 0 评论 0原文

这个问题是因为我读到了这一行通过调用构造函数方法创建对象的新实例。我同意但是构造函数是什么..?一个 OObject 或一个类本身..?.. 抱歉,如果这是一个业余问题,但我仍在学习 java,我发现它很难理解。

这让我想起先有鸡还是先有蛋..?

编辑:

可能是我的问题不清楚,我知道对象是如何创建的,每个类都有一个构造函数,但我想知道的是,堆中的每个对象都有自己的实例变量副本。同样,他们也会有一个构造函数,或者它只是类才有的东西。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

翻了热茶 2024-12-12 20:00:15

在面向对象的设计中,构造函数是根据类定义创建对象的东西...

这里有 2 个关键概念

  • - 类是实例化<的蓝图/em> 对象应该包含行为(方法)和信息(属性)。通常包含一个构造函数。

  • 对象 - 由构造函数创建的东西,实际使用中实例化版本。

使用构造函数的示例

public class MyClass {

    public int intProperty;

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

现在使用类

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

myClassObject.intProperty; // 3

这将创建一个新的 MyClass 对象

Java 会没有类和构造函数就无法工作,它是语言设计模式的核心...

只有类才有构造函数,对象的产品>构造函数,对象本身不包含一个构造函数。

In Object-oriented design, a constructor is what creates an object out of your class definition...

2 key concepts here

  • 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.

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

Java does not work without Classes and Constructors, it is core to the design pattern of the language...

Only Classes have constructors, Objects are the product of Constructors, an Object itself does not contain a constructor.

满天都是小星星 2024-12-12 20:00:15

是的,每个类都有一个构造函数。

如果您没有显式定义一个,Java 将为您创建一个默认的空值。

Yes every class has a constructor.

If you do not explicitly define one, Java will create a default empty one for you.

月亮坠入山谷 2024-12-12 20:00:15

是的,你调用类的构造函数:

MyClass instance = new MyClass();

但请注意,某些对象具有语言允许的特殊构造函数,例如 String:

String x = "foobar";

非常类似于(但不完全相同):

String x = new String("foobar");

注意,如果没有为类定义构造函数,隐含默认(无参数)构造函数。

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 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 .

香草可樂 2024-12-12 20:00:15

在 Java 中,每个类都有一个或多个构造函数,当您使用 new 关键字创建对象时,就会调用该类的构造函数。

例子:

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

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.

example:

Integer i = new Integer("1");
//i is the object, and the Integer class constructor gets called
摘星┃星的人 2024-12-12 20:00:15

每个类都必须有自己的构造函数。您可以提供一个或多个 ctor,或者编译器会为您生成一个默认的无参数 ctor。如果您有自己的构造函数,则编译器不会生成任何内容。

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. If you have your own ctor, than the compiler doesn't generate anything.

失与倦" 2024-12-12 20:00:15

Java 中的每个对象都必须通过某个类的构造函数来创建,除了一些原始类(例如 String),它具有特殊的分配规则。即使在最基本的级别,您也可以随时调用

Object o = new Object();

,并且由于 Java 中的所有对象都继承自 Object 超类,因此大多数对象将继承默认构造函数。

例外情况是当一个类只有一个私有构造函数时——任何外部类都不能调用该构造函数。在这种情况下,因为不再需要默认构造函数,所以它也将不可访问。

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

一般来说,这用于纯静态类(不需要实例)或单例对象(想要限制实例化)。因此,所有对象都是从某个构造函数创建的,但并非所有类都必须具有可用的构造函数。

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. 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.

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). Therefore all objects are created from some constructor, but not all classes necessarily have a usable constructor.

_蜘蛛 2024-12-12 20:00:15

“通过调用构造函数方法创建对象的新实例”这一说法是不正确的说法,也是造成混乱的原因。不存在对象实例这样的东西。对象是类的实例。以下 2 条语句是有效的:

通过调用构造函数方法创建对象。

类的实例(即对象)是通过调用构造函数方法创建的。

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:

An object is created by calling a constructor method.

An instance of a class (i.e. an object) is created by calling a constructor method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文