用Java创建对象 [英] Creating Object in java

查看:67
本文介绍了用Java创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MyObject Object = new MyObject();


这是用Java创建对象的正常方法,但是为什么在创建对象时创建构造器MyObject()?

请解释吗?


This is the normal way to create an object in Java, but why do we create constructor MyObject() at the time of creating object?

Please explain?

推荐答案

我的英语不是最好的,所以我用Wikipedia作答,并认为这很有帮助并解释:

在面向对象的编程中,类中的构造函数(有时简称为ctor)是创建对象时调用的一种特殊类型的子例程.它准备使用新对象,通常接受构造函数用来设置任何对象的参数.首次创建对象时需要的成员变量.之所以称为构造函数,是因为它构造了类的数据成员的值."

因此,这里是准备对象,分配所需的内存等的地方.

如果我理解您的问题错了,对不起.

问候
My English is not the best, so I used Wikipedia for my answer and think this is pretty helpful and explaining:

"In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created. It is called a constructor because it constructs the values of data members of the class."

So it is here to prepare your object, to allocate the needed memory, etc.

If I understood your question wrong I''m sorry.

Greets


不,这一行并不意味着我们在创建对象时就创建了一个构造函数.我们从不这样做.
构造函数定义为一种特殊方法.构造函数的整体思想是定义创建类实例的方法.您可能需要从一开始就学习OOP的基础知识.

-SA
No, this line does not mean we create a constructor at the time of creation of the object. We don''t do it, ever.
The constructor is defined as a special method. The whole idea of constructors is to define a recipe for creation of the instance of a class. You need to learn very basics of OOP, probably from the very beginning.

—SA


我强烈建议这些教程 [^ ] ,它将回答这个问题以及其他问题,并很好地理解Java的工作原理.
I would strongly recommend these tutorials[^], which will answer this question, and others, and give a good understanding of how Java works.


这篇关于用Java创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆