java的构造函数_Java中的构造函数

构造函数用于初始化对象的状态。与方法类似,构造函数还包含在创建对象时执行的语句集合(即指令)。

什么时候构造函数被调用?

每次使用new()关键字创建对象时,都会调用至少一个构造函数(可能是默认构造函数)以将初始值分配给同一类的数据成员。

构造函数在对象或实例创建时被调用。例如: class Geek

{

.......

// A Constructor

new Geek() {}

.......

}

// We can create an object of above class

// using below statement. This statement

// calls above constructor.

Geek obj = new Geek();

编写规则构造函数: 类的构造函数必须与它所在的类名具有相同的名称。

Java中的构造函数不能是抽象的,最终的,静态的和同步的。

访问修饰符可以用在构造函数声明中来控制它的访问,即哪个其他类可以调用构造函数。

构造函数的类型

Java中有两种类型的构造函数: 无参数构造函数: 没有参数的构造函数被称为默认构造函数。如果我们没有在一个类中定义一个构造函数,那么编译器会为这个类创建默认的构造函数(没有参数)。如果我们用参数或无参数编写构造函数,那么编译器不会创建默认构造函数。

默认构造函数根据类型为对象提供默认值,如0,null等。 // Java Program to illustrate calling a

// no-argument constructor

import java.io.*;

class Geek

{

int num;

String name;

// this would be invoked while object

// of that class created.

Geek()

{

System.out.println("Constructor called");

}

}

class GFG

{

public static void main (String[] args)

{

// this would invoke default constructor.

Geek geek1 = new Geek();

// Default constructor provides the default

// values to the object like 0, null

System.out.println(geek1.name);

System.out.println(geek1.num);

}

}

输出: Constructor called

null

0

参数化构造函数:具有参数的构造函数称为参数化构造函数。如果我们想用你自己的值初始化类的字段,那么使用参数化的构造函数。 // Java Program to illustrate calling of

// parameterized constructor.

import java.io.*;

class Geek

{

// data members of the class.

String name;

int id;

// contructor would initialized data members

// with the values of passed arguments while

// object of that class created.

Geek(String name, int id)

{

this.name = name;

this.id = id;

}

}

class GFG

{

public static void main (String[] args)

{

// this would invoke parameterized constructor.

Geek geek1 = new Geek("adam", 1);

System.out.println("GeekName :" + geek1.name +

" and GeekId :" + geek1.id);

}

}

输出: GeekName :adam and GeekId :1

构造函数是否返回任何值?

在构造函数中没有“返回值”语句,但构造函数返回当前类实例。我们可以在构造函数中写入'return'。

构造函数重载

和方法一样,我们可以通过不同的方式重载构造函数来创建对象。编译器根据参数的数量,参数的类型和参数的顺序来区分构造函数。 // Java Program to illustrate constructor overloading

// using same task (addition operation ) for different

// types of arguments.

import java.io.*;

class Geek

{

// constructor with one argument

Geek(String name)

{

System.out.println("Constructor with one " +

"argument - String : " + name);

}

// constructor with two arguments

Geek(String name, int age)

{

System.out.print("Constructor with two arguments : " +

" String and Integer : " + name + " "+ age);

}

// Constructor with one argument but with different

// type than previous..

Geek(long id)

{

System.out.println("Constructor with one argument : " +

"Long : " + id);

}

}

class GFG

{

public static void main(String[] args)

{

// Creating the objects of the class named 'Geek'

// by passing different arguments

// Invoke the constructor with one argument of

// type 'String'.

Geek geek2 = new Geek("Shikhar");

// Invoke the constructor with two arguments

Geek geek3 = new Geek("Dharmesh", 26);

// Invoke the constructor with one argument of

// type 'Long'.

Geek geek4 = new Geek(325614567);

}

}

输出: Constructor with one argument - String : Shikhar

Constructor with two arguments - String and Integer : Dharmesh 26

Constructor with one argument - Long : 325614567

构造函数与Java中的方法有何不同? 构造函数必须与它所定义的类具有相同的名称,而在java中,方法不是的。

构造函数没有任何返回类型,而方法具有返回类型,或者如果没有返回任何值,则返回void。

构造函数在Object创建时只调用一次,而方法可以被调用任意数量的时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值