Experimental purposes:
The idea of interface programming, which is object-oriented programming, is to design an important class, not to be oriented to specific classes, but to interface, that is, the important data in the design class is the variable of the interface declaration rather than the object of the concrete class declaration.
Dog.java
public class Dog {
dogstate state;//declaring interface variable public
void Cry () {
state.showstate ();//calling function inside the interface
}
public void Setstste (Dogstate s) {
state=s;
}
}
Dogstate.java
Public interface Dogstate {public
abstract void showstate ();
}
Meetenemystate.java
The public class Meetenemystate implements dogstate{//the class that implements the interface
@Override public
void Showstate () {
System.out.println ("I met the enemy barking." ");
}
}
Meetfriendstate.java
The public class Meetfriendstate implements dogstate{//the class that implements the interface
@Override public
void Showstate () {
System.out.println ("Welcome to meet friends shaking their tails.") ");
}
}
Softstate.java
The public class Softlystate implements dogstate{//the class that implements the interface
@Override public
void Showstate () {
System.out.println ("Listen to the master in front of the master." ");
}
}
Test,java
public class Test {public
static void Main (String[]args) {
Dog aqi=new Dog ();
Aqi.setstste (New Softlystate ());/set its state
aqi.cry ();//The Showstate ()
inside the interface is called through the method inside the dog class; Aqi.setstste (New Meetenemystate ());
Aqi.cry ();
Aqi.setstste (New Meetfriendstate ());
Aqi.cry ();
}
Run Result:
Summarize:
The class that implements the interface must implement the method inside the interface, in order to define the interface variable, the method in the interface can be realized according to different parameters.