构造函数是什么意思,它的用途是什么. [英] what is meant by constructor and what is the use of that..

查看:60
本文介绍了构造函数是什么意思,它的用途是什么.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告知构造函数的使用...

Please tell the use of constructor...

推荐答案

构造函数:
类名和方法名相同.当您为该类创建对象时,将直接调用该方法.您不想使用该对象单独调用该方法.
示例:
Constructor:
The class name and the method name are same. When you create an object for the class the method will be called directly. You don''t want to call that method separately using that object.
Example:
Class Praveen
{
public praveen
{

} 
}
class main
{
static void main()
{
praveen p=new praveen();  // the method will be called directly when you create the object.
}
}


请参见此处 [ ^ ]有关类的完整详细信息.
See here[^] for full details of classes.


简单地说,构造函数是在定义类的对象时将调用的函数...
它主要用于初始化数据成员并设置默认值.

示例:
Simply the Constructor is a function that will be called when you define an object of the class...
it mostly used for initiate data members and set default values.

Example:
class Student
{
    private int Id;
    Private String Name;

    private Student()
    { 
       Id = 0;
       Name = "ABC";
    }
}



因此,现在无论何时创建学生对象,它都将采用默认值.
问候



So now when ever you create a Student Object it will take the default values.
Regards


这篇关于构造函数是什么意思,它的用途是什么.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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