Java Foundation Sixth Day

Source: Internet
Author: User

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 {

public abstract void Click ();

public abstract void Dbclick ();

public abstract void keyUp ();

public abstract void KeyDown ();

}


Abstract class Buttonadapter implements Buttonlistener {

public void Dbclick () {};

public void KeyUp () {};

public void KeyDown () {};

}


Class MyButton extends Buttonadapter {

String name;

Public MyButton (String name) {

THIS.name = name;

}

public void Click () {

SYSTEM.OUT.PRINTLN (name);

}

}


Class Button {

Buttonlistener l;

public void AddListener (Buttonlistener l) {

THIS.L = l;

}

public void Click () {

L.click ();

}

}


Class Demo {

public static void Main (string[] arge) {

Button B = New button ();

B.addlistener (New MyButton ("1"));

B.click ();

Button B2 = New button ();

B2.addlistener (New MyButton ("2"));

B2.click ();

}

}

*/


/*


Explain what polymorphism is.

Why can functions be overwritten, and properties cannot be overwritten?

For:

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.

*/


/*

Class Person {

String name;

public void Show () {

SYSTEM.OUT.PRINTLN (name);

}

}

Class Man extends person{

String name;

public void Show () {

System.out.println (Super.name);

SYSTEM.OUT.PRINTLN (name);

}

}



Class Demo {

public static void Main (string[] arge) {

Person p = new Mans ();

P.name = "Ererzizi";

P.show ();

}

}

*/


Java Foundation Sixth Day

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.