This code altogether consists of 4 classes, under a package:
Example 1:
They are car.java,bigcar.java,testcar.java,carinterface.java, from other people's Web pages to learn, their own experience.
Car.java:
PackageCom.su.callback;
PublicInterfaceCAR {
voidstart ();
voidStop ();
}
Declares an interface, does not implement methods, declares methods only. The suction port cannot be instantiated.
Bigcar.java:
Code PackageCom.su.callback;
PublicclassBigcarImplementsCAR {
@Override
PublicvoidStart () {
//TODO auto-generated Method Stub
System.out.println ("bigcar start ...");
}
@Override
PublicvoidStop () {
//TODO auto-generated Method Stub
System.out.println ("Bigcar stop!");
}
}
Using the Bigcar class to implement the interface car, to implement its method, that is, write program structure. Bigcar can be instantiated, and the Bigcar object is car type. When you need to call the interface
Car method, you need to use car object to call car method, but car can not be instantiated, we can implement the car's Bigcar object to call car method.
Testcar.java
PackageCom.su.callback;
PublicclassTestcar {
PublicvoidOpercar (CAR c)
{
C.start ();
C.stop ();
}
}
Method of calling car using the Opecar method.
Testinterface.java
Code PackageCom.su.callback;
PublicclassTestInterface {
PublicStaticvoidMain (string[] args)
{
Testcar TC=NewTestcar ();
Bigcar BC=NewBigcar ();
Tc.opercar (BC);
}
}
In this example, calling the Opercar method requires an argument to be passed in, and the argument needs to be an object that is an instantiated object of Bigcar (the class that implements the interface).
Output Result:
Bigcar start ...
Bigcar stop!
Summary: When we call the method of interface A: Amethod (), by calling the function of the interface parameter: B (a a) to invoke the function of the interface Amethod (), to invoke B, you need to pass the argument in.
After his own research, wrote himself such as the following:
Example 2:
Interface class: Jk.java
PackageCom.su.callback;
PublicInterfaceJK {
//An interface can have a declaration of several methods
PublicvoidMethodA ();
PublicvoidMethodB ();
}
Interface Implementation Class Implementjk.java
Code PackageCom.su.callback;
PublicclassIMPLEMENTJKImplementsJK {
@Override
PublicvoidMethodA () {
//TODO auto-generated Method Stub
System.out.println ("MethodA implementation of the interface JK. ");
}
@Override
PublicvoidMethodB () {
//TODO auto-generated Method Stub
System.out.println ("METHODB implementation of the interface JK. ");
}
}
The class of the method that invokes the interface Djk.java
PackageCom.su.callback;
PublicclassDJK {
PublicStaticvoidMain (String args[]) {
IMPLEMENTJK IMPLEMENTJK=NewIMPLEMENTJK ();
Implementjk.methoda ();
Implementjk.methodb ();
}
}
Just call Mehtoda, just execute MethodA,
Summary: The interface object can call the interface method, the interface of the implementation class object can also be dropped by the interface method
Example 1, another definition of a class Testcar, using the method of this class (with the interface as parameters) to call the interface of two methods, and then another class TestInterface call Testcar a method to reach the calling interface of the two methods of the role.
Example 2, a method of invoking an interface directly with an instance of an interface's implementation class.
Java Interface Simple Example