[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.
Interface Buttonlistener
{
Abstract public void OnClick ();
Abstract public void Ondbclick ();
Abstract public void onKeyUp ();
Abstract public void OnKeyDown ();
}
Implement the less commonly used methods-the rest is the usual method, let its subclasses to implement, it is declared abstract class
Abstract class Buttonadapter implements Buttonlistener
{
public void Ondbclick ()
{
}
public void OnKeyUp ()
{
}
public void OnKeyDown ()
{
}
}
Class Button
{
Buttonlistener lst;
public void Addbuttonlistener (Buttonlistener lst)
{
This.lst = LST;
}
//
public void Click ()
{
Lst.onclick ();
}
}
Class MyListener extends Buttonadapter
//{
public void OnClick ()
//{
System.out.println ("click ...");
//}
//}
Class Interfacelistenerdemo
{
public static void Main (string[] args)
{
Button btn = New button ();
MyListener lst = new MyListener ();
Btn.addbuttonlistener (LST);
Btn.addbuttonlistener (New Buttonadapter () {
public void OnClick ()
{
SYSTEM.OUT.PRINTLN ("Anonymous inner class click ...");
}
});
Btn.click ();
Button btn2 = New button ();
Btn2.addbuttonlistener (New Buttonadapter () {
public void OnClick ()
{
System.out.println ("second button click ...");
}
});
Btn2.click ();
}
}
[Job 2]
------------------------------------
Explain what polymorphism is.
Why can functions be overwritten, and properties cannot be overwritten?
Definition: The various forms of existence of a certain class of things.
For inherited classes, use the parent class type to refer to the object of the child class.
For an interface, you can use an interface to reference an object that is created by a class that implements the interface.
A property is an asset that holds data where the function is behaving, does not hold and stores data, so functions can be overwritten, and properties cannot.
It 18 Palm Job _java Foundation Sixth Day _ interface and adapter mode, polymorphic, internal class