Interface (implements multiple inheritance of C + +, while avoiding virtual inheritance)
Deep copy and shallow copy
Inner class
Agent
interface and generic interface: Provide a set of behavior specifications
public interface comparable<t>
{
int CompareTo (T other)
}
...
int Conpareto<employee other>//Specify T
All methods in the interface automatically belong to public
All domains are automatically public staic
The domain in the interface is automatically declared as public static final
Keywords for inheriting interfaces: implements
1. Interface delivery methods must be implemented in subclasses
2. The method in the interface has no method body
3. The interface does not allow the inclusion of instance fields or static methods, but can contain constants. The domain in the interface is automatically declared as public static final
4. Interfaces do not allow the creation of instances, but can declare interface variables comparable x//is also correct
5. Interface class variables must implement reference interface class object//base class pointer to child class object
You can use instanceof to determine whether an object implements a specific interface
//////////////////////////////
Java.lang.comparable<t>
int Comparato (T Other)//greater than positive, equals 0, less than negative
Java.util.Arrays
static void sort (object[] a)//requires that the parameters in the array must implement the comparable interface and be comparable
Java.lang.Integer
static in compare (int x, int y)
//////////////////////////////
Second, object copy and cloning clone
If you make a simple = for a variable, then two variables refer to the same object, so changing one will cause another change. If you want to create two new objects of the same kind, you need a clone method
Deep copy with shallow copy:
Clone is the Protect method in the superclass object, which does not know the specific information of each class, but copies each domain accordingly. If a class contains a reference variable for another object, then the copied two variables will point to the same referenced object, that is, two objects share a sub-object, which does not go to clone a completely independent new object, that is, the default clone is a shallow copy, there is no clone of the object contained in the object.
Deep copy needs to redefine the Clone method (default to object, and access level is protect)
You need to implement the Clloneable interface and redefine clone as public
To implement a deep copy, all mutable fields must be cloned
The ⑴clone method copies a copy of the object and returns it to the caller. Generally, the clone () method satisfies:
① to any object x, there are X.clone ()!=x//The cloned object is not the same object as the original object
② has X.clone () for any object X. getclass () = =x.getclass ()//Clone the object as the type of the original object
③ if the Equals () method of object x is defined properly, then X.clone (). Equals (x) should be established.
Cloning of objects in ⑵java
① in order to get a copy of the object, we can take advantage of the Clone () method of the Objects class.
② overrides the Clone () method of the base class in the derived class and declares it to be public.
③ in the Clone () method of the derived class, call Super.clone ().
④ implements the Cloneable interface in a derived class.
Example:
public class Employee implements Cloneable
{
private String name;
Private Date Hireday; referencing objects
...
Public employess Clone () throw clonenotsupportexception//display tells the compiler that if the class does not implement the Cloneable interface, an exception will be thrown
{
Employee clone = (employee) The Super.clone;//object class clone method will produce a new object;
Clone.hireday = (Date) hireday.clone ();//Create Sub-object
return clone;
}
}
///////////////////////////////
All array types contain a clone method and are public, you can create two data-like and independent arrays
//////////////////////////////
Third, interface and callback function Java callback with C # delegate, function pointer, in fact, Java and they are still very different
Pending reservation
Java Learning notes (Core Java) 6 interfaces and internal classes