为什么构造函数在编码中很重要? [英] Why a constructor is important in coding ?

查看:61
本文介绍了为什么构造函数在编码中很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

Hye,我的名字是Aina,我是java的初学者。基于我的问题,我想问一下为什么构造函数在编码中很重要?你现在看,我正在研究一个项目,一个简单的项目,其中一个任务是让我有1个构造函数和1个方法,并且我需要获得某些输出。现在的问题是,现在我只对1个类使用1个方法,我仍然得到他们要求的输出。但是,我正在尽力满足项目要求,这也是一个构造函数。因为我缺乏知识,我仍然无法理解构造函数如何与方法一起工作。因此,尽管我已经得到了我想要的输出,但是有一个构造函数很重要吗?



我尝试了什么:



对不起,我无法上传我的作品。我现在至少需要知道我需要知道什么。

解决方案

构造函数很重要,因为它们让你 - 程序员 - 确保实例是完整的准备好在用户开始玩之前完成这项工作。



例如,我的默认表单构造函数从配置文件中读取它的大小,位置和布局信息应用它,以便在显示表单时,它出现在上次相同的位置 - 即使它最后一次被其他应用程序使用。



这是一个非必要的示例,但构造函数可以为变量分配内存,创建字体,从数据库中检索数据,或者在实例为业务做好准备之前需要完成的任何类。例如,您可能有一个Car类,它将注册号作为构造函数参数,并在实例传回给用户之前访问互联网以查找品牌,型号和颜色。



我不能具体说明你想要的内容 - 我不知道你的作业涉及什么 - 但是在课程开始之前需要完成的东西在构造函数中,可选的东西可以进入方法。


查看相关的 Wikipedia 页面:构造函数(面向对象编程) [ ^ ]。


此页:Lesson:类和对象(Java™教程>学习Ja va语言) [ ^ ]。

Greetings,
Hye, My name is Aina and I'm a beginner in java. Based on my question, I would like to ask why a constructor is important in coding? You see now, i'm working on a project, a simple one, and one of the task is for me to have 1 constructor and 1 method, and certain output i need to get. The problem now is that, for now I only use 1 method for 1 class and i still get the output they asked for. But, I'm trying my best to meet the project requirements which to also have a constructor. For I am lack of knowledge, I still can't understand of how constructor work alongside a method. So is it important to have a constructor despite the fact that i already get my desired output ?

What I have tried:

Sorry I can't upload my work here. I just need to know of what I need to know, for now at least.

解决方案

Constructors matter because they let you - the programmer - ensure that the instance is fully ready to do the job before the user starts to play with it.

For example, my default form constructor reads it's size, position and layout information from a configuration file and applies it so that when the form is displayed, it appears in the same place it was last time - even if it was last used by a different application.

That's a non-essential example, but constructors can allocate memory for variables, create fonts, retrieve data from databases, or whatever the class needs done before an instance is ready for business. For example, you might have a Car class that takes a registration number as it's constructor parameter and accesses the internet to find out the make, model, and colour before the instance is passed back to the user.

I can't be specific as to what you want in your constrictor - I have no idea what your homework is involving - but stuff that needs to be done before the class can be used goes in the constructor, optional stuff can go in methods.


Have a look at the related Wikipedia page: Constructor (object-oriented programming)[^].


And this page: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)[^].


这篇关于为什么构造函数在编码中很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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