java构造函数_Java构造函数
java构造函数
Java构造函数 (Constructors in Java)
A constructor is a special method that is used to initialize an object. Every class has a constructor either implicitly or explicitly.
构造函数是一种用于初始化对象的特殊方法。 每个类都有一个隐式或显式的构造函数。
If we don't declare a constructor in the class then JVM builds a default constructor for that class. This is known as default constructor.
如果我们不在类中声明构造函数,那么JVM将为该类构建默认构造函数。 这称为默认构造函数 。
A constructor has same name as the class name in which it is declared. Constructor must have no explicit return type. Constructor in Java can not be abstract, static, final or synchronized. These modifiers are not allowed for constructor.
构造函数的名称与声明其的类名称相同 。 构造函数必须没有显式的返回类型 。 Java中的构造函数不能是abstract,static,final或sync 。 构造函数不允许使用这些修饰符。
Syntax to declare constructor
声明构造函数的语法
className (parameter-list){
code-statements
}
className is the name of class, as constructor name is same as class name.
className是类的名称,因为构造函数名称与类名称相同 。
parameter-list is optional, because constructors can be parameterize and non-parameterize as well.
parameter-list是可选的,因为构造函数可以同时进行参数化和非参数化。
构造函数示例 (Constructor Example )
In Java, constructor structurally looks like given in below program. A Car class has a constructor that provides values to instance variables.
在Java中,构造函数的结构类似于下面的程序。 Car类具有为实例变量提供值的构造函数。
class Car
{
String name ;
String model;
Car( )//Constructor
{
name ="";
model="";
}
}
构造函数的类型 (Types of Constructor)
Java Supports two types of constructors:
Java支持两种类型的构造函数:
- Default Constructor 默认构造函数
- Parameterized constructor 参数化构造函数
Each time a new object is created at least one constructor will be invoked.
每次创建新对象时,将至少调用一个构造函数。
Car c = new Car()//Default constructor invoked
Car c = new Car(name); //Parameterized constructor invoked
默认构造函数 (Default Constructor)
In Java, a constructor is said to be default constructor if it does not have any parameter. Default constructor can be either user defined or provided by JVM.
在Java中,如果构造函数没有任何参数,则称为默认构造函数。 默认构造函数可以是用户定义的,也可以由JVM提供。
If a class does not contain any constructor then during runtime JVM generates a default constructor which is known as system define default constructor.
如果一个类不包含任何构造函数,那么在运行时JVM会生成一个默认构造函数,称为系统定义默认构造函数。
If a class contain a constructor with no parameter then it is known as default constructor defined by user. In this case JVM does not create default constructor.
如果一个类包含没有参数的构造函数,则它被称为用户定义的默认构造函数。 在这种情况下,JVM不创建默认构造函数。
The purpose of creating constructor is to initialize states of an object.
创建构造函数的目的是初始化对象的状态 。
The below image shows how JVM adds a constructor to the class during runtime.
下图显示了JVM在运行时如何向该类添加构造函数。
用户定义默认构造函数 (User Define Default Constructor)
Constructor which is defined in the class by the programmer is known as user-defined default constructor.
程序员在类中定义的构造函数称为用户定义的默认构造函数。
Example:
例:
In this example, we are creating a constructor that has same name as the class name.
在此示例中,我们将创建一个与类名称同名的构造函数。
class AddDemo1
{
AddDemo1()
{
int a=10;
int b=5;
int c;
c=a+b;
System.out.println("*****Default Constructor*****");
System.out.println("Total of 10 + 5 = "+c);
}
public static void main(String args[])
{
AddDemo1 obj=new AddDemo1();
}
}
构造函数重载 (Constructor Overloading)
Like methods, a constructor can also be overloaded. Overloaded constructors are differentiated on the basis of their type of parameters or number of parameters. Constructor overloading is not much different than method overloading. In case of method overloading you have multiple methods with same name but different signature, whereas in Constructor overloading you have multiple constructor with different signature but only difference is that constructor doesn't have return type.
像方法一样,构造函数也可以重载。 重载的构造函数根据其参数类型或参数数量进行区分。 构造函数重载与方法重载没有太大区别。 在方法重载的情况下,您有多个具有相同名称但签名不同的方法,而在Constructor重载中,您有多个具有不同签名的构造函数,但唯一的区别是构造函数没有return type 。
构造函数重载示例 (Example of constructor overloading)
class Cricketer
{
String name;
String team;
int age;
Cricketer ()//default constructor.
{
name ="";
team ="";
age = 0;
}
Cricketer(String n, String t, int a) //constructor overloaded
{
name = n;
team = t;
age = a;
}
Cricketer (Cricketer ckt) //constructor similar to copy constructor of c++
{
name = ckt.name;
team = ckt.team;
age = ckt.age;
}
public String toString()
{
return "this is " + name + " of "+team;
}
}
Class test:
{
public static void main (String[] args)
{
Cricketer c1 = new Cricketer();
Cricketer c2 = new Cricketer("sachin", "India", 32);
Cricketer c3 = new Cricketer(c2 );
System.out.println(c2);
System.out.println(c3);
c1.name = "Virat";
c1.team= "India";
c1.age = 32;
System .out. print in (c1);
}
}
this is sachin of india this is sachin of india this is virat of india
这是印度的萨钦这是印度的萨钦这是印度的维拉特
构造器链接 (Constructor Chaining)
Constructor chaining is a process of calling one constructor from another constructor in the same class. Since constructor can only be called from another constructor, constructor chaining is used for this purpose.
构造函数链接是一个从同一类中的另一个构造函数调用一个构造函数的过程。 由于只能从另一个构造函数中调用构造函数,因此为此使用了构造函数链接。
To call constructor from another constructor this keyword is used. This keyword is used to refer current object.
要从另一个构造函数调用构造函数,请使用此关键字 。 此关键字用于引用当前对象。
Lets see an example to understand constructor chaining.
让我们看一个示例,以了解构造函数链接。
class Test
{
Test()
{
this(10);
}
Test(int x)
{
System.out.println("x="+x);
}
public static void main(String arg[])
{
Test object = new Test();
}
}
x=10
x = 10
Constructor chaining is used when we want to perform multiple tasks by creating a single object of the class.
当我们想通过创建类的单个对象来执行多个任务时,使用构造函数链接。
In the below image, we have described the flow of constructor calling in the same class.
在下图中,我们描述了在同一类中调用构造函数的流程。
Example:
例:
Lets see one more example to understand the constructor chaining. Here we have created three constructors and calling them using by using this keyword.
让我们再看一个示例,以了解构造函数链接。 在这里,我们创建了三个构造函数,并使用此关键字调用它们。
class abc
{
public abc()
{
this(5);
System.out.println("Default Constructor");
}
public abc(int x)
{
this(5, 6);
System.out.println("Constructor with one Parameter");
System.out.println("Value of x ==> "+x);
}
public abc(int x, int y)
{
System.out.println("Constructor with two Parameter");
System.out.println("Value of x and y ==> "+x+" "+y);
}
}
class ChainingDemo1
{
public static void main(String as[])
{
abcobj = new abc();
}
}
私人建筑商 (Private Constructors)
In Java, we can create private constructor to prevent class being instantiate. It means by declaring a private constructor, it restricts to create object of that class.
在Java中,我们可以创建私有构造函数来防止类被实例化 。 这意味着通过声明一个私有构造函数,它限制了创建该类的对象。
Private constructors are used to create singleton class. A class which can have only single object known as singleton class.
私有构造函数用于创建单例类 。 只能具有单个对象的类称为单例类。
In private constructor, only one object can be created and the object is created within the class and also all the methods are static. An object can not be created if a private constructor is present inside a class. A class which have a private constructor and all the methods are static then it is called Utility class.
在私有构造函数中,只能创建一个对象,并且该对象是在类中创建的,并且所有方法都是静态的。 如果类中存在私有构造函数,则无法创建对象。 一个具有私有构造函数且所有方法都是静态的类 ,因此称为实用程序类 。
Example:
例:
final class abc
{
private abc()
{}
public static void add(int a, int b)
{
int z = a+b;
System.out.println("Addition: "+z);
}
public static void sub(int x, int y)
{
int z = x-y;
System.out.println("Subtraction: "+z);
}
}
class PrivateConDemo
{
public static void main(String as[])
{
abc.add(4, 5);
abc.sub(5, 3);
}
}
翻译自: https://www.studytonight.com/java/constructor-in-java.php
java构造函数