Copy Constructor in Java

Reference: TutorialPoints, GeekforGeeks

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to:

  • Initialize one object from another of the same type.

  • Copy an object to pass it as an argument to a function.

  • Copy an object to return it from a function.

In C++, if a copy constructor is not defined in a class, the compiler itself defines one.

Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.

 1 class Complex {
 2  
 3     private double re, im;
 4      
 5     // A normal parametrized constructor 
 6     public Complex(double re, double im) {
 7         this.re = re;
 8         this.im = im;
 9     }
10      
11     // copy constructor
12     Complex(Complex c) {
13         System.out.println("Copy constructor called");
14         re = c.re;
15         im = c.im;
16     }
17       
18     // Overriding the toString of Object class
19     @Override
20     public String toString() {
21         return "(" + re + " + " + im + "i)";
22     }
23 }
24  
25 public class Main {
26  
27     public static void main(String[] args) {
28         Complex c1 = new Complex(10, 15);
29          
30         // Following involves a copy constructor call
31         Complex c2 = new Complex(c1);   
32  
33         // Note that following doesn't involve a copy constructor call as 
34         // non-primitive variables are just references.
35         Complex c3 = c2;   
36  
37         System.out.println(c2); // toString() of c2 is called here
38     }
39 }

Output:

Copy constructor

called (10.0 + 15.0i)

 1 class Complex {
 2  
 3     private double re, im;
 4  
 5     public Complex(double re, double im) {
 6         this.re = re;
 7         this.im = im;
 8     }
 9 }
10  
11 public class Main {
12      
13     public static void main(String[] args) {
14         Complex c1 = new Complex(10, 15);  
15         Complex c2 = new Complex(c1);  // compiler error here
16     }
17 }

Compile error

 

转载于:https://www.cnblogs.com/ireneyanglan/p/4941395.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在面向对象编程中,构造函数(constructor)是一种特殊的成员函数,用于创建和初始化类的对象。构造函数的名称与类名相同,并且没有返回类型。当创建一个类的对象时,构造函数会自动调用,并且可以执行一些初始化操作。 构造函数有以下几个特点: 1. 构造函数在对象创建时自动调用,无需手动调用。 2. 构造函数可以有参数,用于接收初始化对象时传递的值。 3. 构造函数可以重载,即一个类可以有多个不同参数列表的构造函数。 4. 如果没有显式定义构造函数,编译器会自动生成一个默认构造函数。 构造函数的作用主要有两个: 1. 分配内存空间:构造函数负责为对象分配内存空间,确保对象有足够的内存来存储其成员变量。 2. 初始化对象:构造函数可以对对象的成员变量进行初始化,确保对象在创建后处于一个合理的初始状态。 下面是一个示例代码,展示了一个简单类的构造函数的定义和使用: ```cpp #include <iostream> class MyClass { public: int value; // 默认构造函数 MyClass() { value = 0; std::cout << "Default constructor called" << std::endl; } // 带参数的构造函数 MyClass(int v) { value = v; std::cout << "Parameterized constructor called" << std::endl; } }; int main() { MyClass obj1; // 调用默认构造函数 std::cout << "obj1.value: " << obj1.value << std::endl; MyClass obj2(10); // 调用带参数的构造函数 std::cout << "obj2.value: " << obj2.value << std::endl; return 0; } ``` 输出结果: ``` Default constructor called obj1.value: 0 Parameterized constructor called obj2.value: 10 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值