java语言基础学习-----接口与对象克隆

一、接口

1.在java语言中,接口是一组对类的需求描述(规则协议),实现该接口的类必须遵循这一协议。

     an interface is not a class but a set of requirements for classes that want to conform to the interface

 

2.在接口中可以定义常量。但是接口中不能有实例域,也不能在方法中实现方法。

 

3.一个类实现一个接口,必须实现其中的所有方法。

 

4.在接口定义中,接口中的方法声明自动为public,实现此接口的类中的对应的方法必须声明为public的,不然编译器将认为该方法是包访问级别的。

 

5.Interfaces and Abstract Classes(接口和抽象类)比较

在abstract class方式中,抽象类中可以有自己的数据成员,也可以有非abstarct的成员方法,而在interface方式的实现中,接口中只能够有静态的 不能被修改的数据成员(也就是必须是static final的,不过在interface中一般不定义数据成员),所有的成员方法都是abstract的。从某种意义上说,interface是一种特殊 形式的abstract class。

   (1).一个类可以实现多个接口,但是只能继承一个(抽象)类

   (2).实现一个接口,必须实现所有的方法;相比,继承一个抽象类,不必实现所有的抽象方法,子类可以实现父类的个别抽象方法,不过这样的子类也是抽像类。

 

二、Object Cloning(对象克隆)

 

1.Copying and cloning

  

  

 

COPYING

 

Employee original = new Employee("John Public", 50000);
Employee copy = original;
copy.raiseSalary(10); // oops--also changed original

   CLONING

   Employee copy = original.clone();
   copy.raiseSalary(10); // OK--original unchanged

 

2.The default cloning operation is "shallow"—it doesn't clone objects that are referenced inside other objects.

 

 

 

 

 Does it matter if the copy is shallow? It depends. If the subobject that is shared between the original and the shallow clone is immutable, then the sharing is safe. This certainly happens if the subobject belongs to an immutable class, such as String. Alternatively, the subobject may simply remain constant throughout the lifetime of the object, with no mutators touching it and no methods yielding a reference to it.

如果对象是不改变的,那么这种共享是安全的,如String.

Quite frequently, however, subobjects are mutable, and you must redefine the clone method to make a deep copy that clones the subobjects as well. In our example, the hireDay field is a Date, which is mutable.如果对象是要改变的,那么就要自定义clone方法了。

For every class, you need to decide whether

  •  
    • The default clone method is good enough;
    • The default clone method can be patched up by calling clone on the mutable subobjects;
    • clone should not be attempted.

 

The third option is actually the default. To choose either the first or the second option, a class must

  •  
    • Implement the Cloneable interface, and
    • Redefine the clone method with the public access modifier.

 A subclass can call a protected clone method only to clone its own objects. You must redefine clone to be public to allow objects to be cloned by any method.

 子类只能调用受保护的clone方法克隆它自己。必须重新定义clone方法,才能够在任何方法中克隆对象。

 

 

Even if the default (shallow copy) implementation of clone is adequate, you still need to implement the Cloneable interface, redefine clone to be public, and call super.clone().

 都要实现Cloneable接口。

 

 1. import java.util.*;
 2.
 3. public class CloneTest
 4. {
 5.    public static void main(String[] args)
 6.    {
 7.       try
 8.       {
 9.          Employee original = new Employee("John Q. Public", 50000);
10.          original.setHireDay(2000, 1, 1);
11.          Employee copy = original.clone();
12.          copy.raiseSalary(10);
13.          copy.setHireDay(2002, 12, 31);
14.          System.out.println("original=" + original);
15.          System.out.println("copy=" + copy);
16.       }
17.       catch (CloneNotSupportedException e)
18.       {
19.          e.printStackTrace();
20.       }
21.    }
22. }
23.
24. class Employee implements Cloneable
25. {
26.    public Employee(String n, double s)
27.    {
28.       name = n;
29.       salary = s;
30.    }
31.
32.    public Employee clone() throws CloneNotSupportedException
33.    {
34.       // call Object.clone()
35.       Employee cloned = (Employee)super.clone();
36.
37.       // clone mutable fields
38.       cloned.hireDay = (Date)hireDay.clone();// deep clone
39.
40.       return cloned;
41.    }
42.
43.    /**
44.       Set the hire day to a given date
45.       @param year the year of the hire day
46.       @param month the month of the hire day
47.       @param day the day of the hire day
48.    */
49.    public void setHireDay(int year, int month, int day)
50.    {
51.       hireDay = new GregorianCalendar(year, month - 1, day).getTime();
52.    }
53.
54.    public void raiseSalary(double byPercent)
55.    {
56.       double raise = salary * byPercent / 100;
57.       salary += raise;
58.    }
59.
60.    public String toString()
61.    {
62.       return "Employee[name=" + name
63.          + ",salary=" + salary
64.          + ",hireDay=" + hireDay
65.          + "]";
66.    }
67.
68.    private String name;
69.    private double salary;
70.    private Date hireDay;
71. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值