Java Foundation Sixth Day _ interface and adapter mode, polymorphic, inner class

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.