[Job 1]
------------------------------------
Use abstract classes and interfaces to design the adapter mode. The involved classes and interfaces are buttonlistener (interfaces ),
It contains methods such as click (), dbclick (), keyup (), and keydown.
Buttonadapter (Button adapter class), which defaults to non-click () methods.
Add the addlistener (buttonlistener L) method to the button class.
Code:
// Button listener Interface
Interface buttonlistener {
Public void onclick (); // click the event
Public void ondbclick (); // double-click the event
Public void onkeydown (); // press the key
Public void onkeyup (); // press the button to release
}
// Button
Class button {
// Button listener
Private buttonlistener listener;
// Add listener
Public void addlistener (buttonlistener LSN ){
This. Listener = lsn;
}
// Simulate a click event
Public void click (){
// Process the Click Event
Listener. onclick ();
}
// Simulate double-click events
Public void dbclick (){
Listener. ondbclick ();
}
}
// Button listener Adapter
Abstract class buttonlisteneradapter implements buttonlistener {
Public void ondbclick (){
}
Public void onkeyup (){
}
Public void onkeydown (){
}
}
Class mylistener extends buttonlisteneradapter {
Public void onclick (){
System. Out. println ("I click one! ");
}
}
Class yourlistener extends buttonlisteneradapter {
Public void onclick (){
System. Out. println ("You click one! ");
}
}
Class adapterdemo {
Public static void main (string [] ARGs ){
// Button1
Button btn1 = new button ();
Mylistener L = new mylistener ();
Btn1.addlistener (L );
Btn1.click ();
// Button2
Button btn2 = new button ();
Yourlistener yl = new yourlistener ();
Btn2.addlistener (yl );
Btn2.click ();
}
}
[Job 2]
------------------------------------
Describes what polymorphism is.
Why can I overwrite a function but not an attribute?
1) multiple forms.
For an inherited class, the parent class type is used to reference the object of the subclass.
The parent class calls subclass objects between classes with inheritance relationships and calls methods from different angles.
For an interface, you can use the interface reference to create an object for the class that implements the interface.
2) A function is a process or action. After the subclass inherits the parent class, it can override the parent class method,
After the corresponding content is called, it ends through the stack;
However, attributes belong to assets and store data. Therefore, functions can be overwritten, but attributes cannot.
This article is from the "11138113" blog. For more information, contact the author!
Contents of the sixth day of big data