拷贝构造函数 示例_带有示例的Java构造函数

拷贝构造函数 示例

Java构造函数 (Java Constructor)

  • A constructor is a special function or method to initialize an object when it is created.

    构造函数是一种特殊的函数或方法,用于在创建对象时对其进行初始化。

  • Constructor name must be the same as the class name.

    构造函数名称必须与类名称相同。

  • The syntax of the constructor is similar as method except constructors have no explicit return type.

    构造函数的语法与方法相似,只是构造函数没有显式的返回类型。

构造目的 (Purpose of Constructor)

The purpose of a constructor is to assign initial values to the instance variables at runtime.

构造函数的目的是在运行时将初始值分配给实例变量。

构造函数的类型 (Types of Constructors)

There are two types of constructor in java and the name of those constructors given below:

Java中有两种类型的构造函数,下面给出了这些构造函数的名称:

  1. Default Constructor or No argument Constructor

    默认构造函数或无参数构造函数

  2. Parameterized Constructor

    参数化构造函数

1)默认构造函数或无参数构造函数 (1) Default Constructor or No argument Constructor)

Here we will discuss in detail...

在这里,我们将详细讨论...

  • Default Constructor or No argument constructor is that constructor which takes no argument.

    默认构造函数或无参数构造函数是不带参数的构造函数。

  • In this constructor, we don’t pass any argument or parameter.

    在此构造函数中,我们不传递任何参数或参数。

  • When we don’t include any constructor in the class so java compiler calls this constructor by default that is the name of this constructor is default constructor.

    当我们在类中不包含任何构造函数时,java编译器会默认调用该构造函数,即该构造函数的名称为默认构造函数。

Syntax:

句法:

    class Classname{
        Classname(){
            // initialization of instance variable
        }
    }

Example:

例:

import java.util.*;

// Class Declaration
class Constr {
    // Declaring str instance variable of String type
    String str;
    // Constructor Definition
    Constr() {
        str = "Hello World!!";
    }
}

public class Main {
    public static void main(String[] args) {
        // Constructor Calling
        Constr con = new Constr();
        System.out.println(con.str);
    }

}

Output

输出量

D:\Programs>javac Main.java
D:\Programs>java Main
Hello World !!

2)参数化构造函数 (2) Parameterized Constructor)

Here we will discuss in detail...

在这里,我们将详细讨论...

  • Parameterized Constructor are those constructor which takes argument explicitly.

    参数化构造函数是显式接受参数的那些构造函数。

  • In this constructor we must pass argument or parameter.

    在此构造函数中,我们必须传递参数或参数。

  • When we include both constructor (Default and Parameterized) in the class so java compiler does not call this constructor bydefault (i.e. this constructor will be called explicitly).

    当我们在类中同时包含构造函数(Default和Parameterized)时,java编译器不会默认调用该构造函数(即,将显式调用此构造函数)。

Syntax:

句法:

    class Classname{
        Classname(datatype arg1, datatype arg2, ...){
            // initialization of instance variable
        }
    }

Example:

例:

import java.util.*;

// Class Declaration
class ParamConstr {
    // Instance Variable
    String str;
    // Constructor Definition
    ParamConstr(String stri) {
        str = stri;
    }
}

public class Main {
    public static void main(String[] args) {
        // Calling Parameterized Constructor
        ParamConstr pcon = new ParamConstr("Hi, Welcome in parametrized constructor");
        System.out.println(pcon.str);
    }
}

Output

输出量

D:\Programs>javac Main.java

D:\Programs>java Main
Hi, Welcome in parameterized constructor


翻译自: https://www.includehelp.com/java/java-constructors-with-examples.aspx

拷贝构造函数 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值