Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: How to clone objects

Java: How to clone objects
{filelink=11065}

public class Persons implements Cloneable
 {
  private String name;
  private double salary;
  public Object clone()
  	{
    Persons obj = new Persons(this.name, this.salary);
    return obj;
  }
  public Persons(String nm, double sal)
   {
      name=nm;
      salary=sal;
   }
  protected void update_Salary(double new_salary)
  {
    salary =new_salary;
  }
  public double getSalaire()
  {
  	return salary;
  }
    public String getName()
  {
  	return name;
  }
}
class Main
{
  public static void main(String[] args)
  	{
     // Create an object of "Person" class
    Persons person1 = new Persons("Bill Gate", 50000.000);
    // Cloning object 'person1'
    Persons person2 = (Persons) person1.clone();
    Persons person3 = (Persons) person2.clone();
    System.out.println("Persons 1: " + person1.getName() + " " + person1.getSalaire());
    System.out.println("Persons 2: " + person2.getName() + " " + person2.getSalaire());
    System.out.println("Persons 3: " + person3.getName() + " " + person3.getSalaire());
  }
}

Users who found this page were searching for:

  • content
  • +python xml import to mssql
  • python is easy to use interface
  • how to read yahoo mail by java code
  • java yahoo gmail pop3
  • java mail clone message
  • how to print out a copied message in java
  • java awt font font underline

No comments yet.

Leave a Comment