0
how to copy one object to another object without or with using copy constructor?
4 Antworten
+ 1
This could be by copy constructor.
Suppose the class is Person, its copy constructor could be:
public Person(Person p){
this(p.getAge());
}
have a look at example in below:
ex:
class Person{
private int age;
public Person(int age){
this.age = age;
}
// * Copy constructor *
public Person(Person p){
this(p.getAge());
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
public class Program
{
public static void main(String[] args) {
Person p1 = new Person(17);
Person p2 = new Person(p1);
p1.setAge(25);
System.out.println("p1 age = " +p1.getAge());
System.out.println("p2 age = " +p2.getAge());
}
}
0
objects reference
0
plz give an example...
0
thnx...