拷贝构造函数就是构造函数的参数的类型是该构造函数所在的类,即参数就是该类的一个对象。
Example:
1.Clock类:
public class Clock {
private int hour;
private int minute;
private int second;
public Clock(){
setTime(0,0,0);
}
public Clock(int h,int m,int s){
setTime(h,m,s);
}
/*
* 拷贝构造函数
*/
public Clock(Clock clock){
this.hour=clock.hour;
this.minute=clock.minute;
this.second=clock.second;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
public void setTime(int h,int m,int s){
if(h>=0 && h<24)
this.hour=h;
else
this.hour=0;
if(m>=0 && m<60)
this.minute=m;
else
this.minute=0;
if(s>=0 && s<60)
this.second=s;
else
this.second=0;
}
public void printTime(){
if(this.hour<10)
System.out.print("0");
System.out.print(this.hour+":");
if(this.minute<10)
System.out.print("0");
System.out.print(this.minute+":");
if(this.second<10)
System.out.print("0");
System.out.println(this.second);
}
}
2.main函数:
public class Program {
public static void main(String[] args) {
Clock c1=new Clock(6,43,23);
Clock c2=new Clock(c1);//调用拷贝构造函数
c1.printTime();
c2.printTime();
}
}
3.运行结果:
06:43:23
06:43:23