From http://www.cnblogs.com/ggjucheng/archive/2012/12/04/2802301.html
English from http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html
When defining a new interface, it is to define a new reference data type. You can use the interface name anywhere, just like the names of other classes. If you define a reference variable of the interface type and assign a value to the class of the object of this variable, you must implement this interface.
For example, here is a method to find the largest object from a pair of objects. The classes corresponding to these objects have been implementedRelatable
:
Public object findlargest (Object object1, object object2) {relatable obj1 = (relatable) object1; relatable obj2 = (relatable) object2; If (obj1 ). islargerthan (obj2)> 0) return object1; else return object2 ;}
Convert object1Relatable, you can callIslargerthan
Method.
If you useRelatable
Many classes are implemented. Objects instantiated from any of these classes can be passed throughThe findlargest () method compares the size. Of course, the two objects provided are the same class. Similarly, they can all be compared using the following method:
Public object findsmallest (Object object1, object object2) {relatable obj1 = (relatable) object1; relatable obj2 = (relatable) object2; If (obj1 ). islargerthan (obj2) <0) return object1; else return object2;} public Boolean isequal (Object object1, object object2) {relatable obj1 = (relatable) object1; relatable obj2 = (relatable) object2; If (obj1 ). islargerthan (obj2) = 0) return true; else return false ;}
These methods apply to any "relatable" object, no matter which class they inherit. When they implementRelatable
They can be their own type andRelatable type. This gives some advantages of multi-inheritance, so that their behavior inherits from the parent class and interface.