[Job 1]
------------------------------------
Implement adapter pattern design using abstract classes and interfaces. The classes and interfaces involved are buttonlistener (interfaces), respectively,
It contains the click ()/Dbclick ()/keyUp ()/KeyDown () methods.
Buttonadapter (Button adapter class), the default implementation of the non-click () method in the adapter.
Add the AddListener (Buttonlistener L) method to the Button class.
For:
/*
Define an interface to allow the received value to determine the corresponding action, the value received by the interface needs a monitor to receive
Note the problem: a. The interface implementation is all implemented even if the empty implementation. B. Define a monitor to receive the value of the interface class
*/
Defining interfaces
Interface buttonlistener{
public void click ();
public void Dbclick ();
public void keyUp ();
public void KeyDown ();
}
Define the default implementation of the button adapter class adapter for non-click methods
Abstract class Buttonaspter implements buttonlistener{
public void Click () {}
public void Dbclick () {}
public void KeyUp () {}
public void KeyDown () {}
}
If it is clicked, implement the Click event
Class ButtonClick extends buttonaspter{
public void Click () {
System.out.println ("click");
}
}
If double-click, implement double-click event
Class Buttondbclick extends buttonaspter{
public void Dbclick () {
System.out.println ("Double-click");
}
}
Listener
Class button{
Private Buttonlistener ll;
Add Listener
public void AddListener (Buttonlistener ll) {
This.ll=ll;
}
Simulate Click events
public void Click () {
Ll.click ();
}
Simulate double-click events
public void Dbclick () {
Ll.dbclick ();
}
}
Class adaptdemo{
public static void Main (string[] args) {
Click
button Bt=new button ();
ButtonClick bb=new ButtonClick ();
Bt.addlistener (BB);
Bt.click ();
Double click
button Bt2=new button ();
Buttondbclick bb2=new Buttondbclick ();
Bt2.addlistener (BB2);
Bt2.dbclick ();
}
}
[Job 2]
------------------------------------
Explain what polymorphism is.
Why can functions be overwritten, and properties cannot be overwritten?
A. The same message can give different behavior according to different objects. Different forms of the same thing.
B. For an object property is itself something, belonging to the asset is not covered, and the function is the behavior method, does not contain data, can be overwritten.
Java Foundation Sixth Day _ interface and adapter mode, polymorphic, inner class