File tree Expand file tree Collapse file tree 5 files changed +150
-0
lines changed
src/main/java/com/hmkcode Expand file tree Collapse file tree 5 files changed +150
-0
lines changed Original file line number Diff line number Diff line change 1+ Using Lambda to Implement Funtional Interface Method
2+ ====================================================
3+
4+ - We have a * virtual* ` Button ` that when clicked will call an abstract method ** onClick()** of a listener interface ` OnClickListener ` .
5+ - We need to implement the ** onClick()** so that it prints the ` Button ` name.
6+
7+
8+ ` OnClickListener.java `
9+
10+ ``` java
11+ package com.hmkcode ;
12+
13+ public interface OnClickListener {
14+ void onClick (Button button );
15+ }
16+ ```
17+
18+ ` Button.java `
19+
20+ ``` java
21+ package com.hmkcode ;
22+
23+ public class Button {
24+
25+ private OnClickListener onClickListener;
26+ private String name;
27+
28+ // click the button
29+ public void click (){
30+ this . onClickListener. onClick(this );
31+ }
32+
33+ // getters & setters
34+ }
35+ ```
36+
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+
3+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.hmkcode</groupId >
8+ <artifactId >java-lambda</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <name >java-lambda</name >
12+
13+ <build >
14+ <plugins >
15+ <plugin >
16+ <artifactId >maven-compiler-plugin</artifactId >
17+ <version >3.8.0</version >
18+ <configuration >
19+ <source >1.8</source >
20+ <target >1.8</target >
21+ </configuration >
22+ </plugin >
23+ <plugin >
24+ <groupId >org.codehaus.mojo</groupId >
25+ <artifactId >exec-maven-plugin</artifactId >
26+ <version >1.6.0</version >
27+ <configuration >
28+ <mainClass >com.hmkcode.App</mainClass >
29+ </configuration >
30+ </plugin >
31+ </plugins >
32+ </build >
33+
34+ </project >
Original file line number Diff line number Diff line change 1+ package com .hmkcode ;
2+
3+
4+ public class App implements OnClickListener
5+ {
6+ public static void main ( String [] args ){
7+ System .out .println ( "Running App..." );
8+ new App ().run ();
9+ }
10+
11+ public void run (){
12+
13+ Button myButton = new Button ();
14+ myButton .setName ("MyButton" );
15+
16+ // 1. implements onClickListener
17+ //myButton.setOnClickListener(this);
18+
19+ // 2. anonymous class
20+ /*myButton.setOnClickListener(new OnClickListener() {
21+ @Override
22+ public void onClick(Button button) {
23+ System.out.println(button.getName() +" Clicked! - anonymous class");
24+ }
25+ }); */
26+
27+ // 3. lambda
28+ OnClickListener lambda = button -> { System .out .println (button .getName ()+" Clicked! - lambda" ); } ;
29+ myButton .setOnClickListener (lambda );
30+
31+
32+ // click the button
33+ myButton .click ();
34+ }
35+
36+
37+ @ Override
38+ public void onClick (Button button ) {
39+ System .out .println (button .getName () +" Clicked! - implements interface" );
40+
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ package com .hmkcode ;
2+
3+ public class Button {
4+
5+ private OnClickListener onClickListener ;
6+ private String name ;
7+
8+ public void click (){
9+ this .onClickListener .onClick (this );
10+ }
11+
12+
13+ public String getName () {
14+ return name ;
15+ }
16+
17+ public void setName (String name ) {
18+ this .name = name ;
19+ }
20+
21+ public OnClickListener getOnClickListener () {
22+ return onClickListener ;
23+ }
24+
25+ public void setOnClickListener (OnClickListener onClickListener ) {
26+ this .onClickListener = onClickListener ;
27+ }
28+
29+
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .hmkcode ;
2+
3+
4+ public interface OnClickListener {
5+ void onClick (Button button );
6+
7+ }
You can’t perform that action at this time.
0 commit comments