Skip to content

Commit c15471e

Browse files
committed
update README.md
update README.md
1 parent 3f36d4f commit c15471e

File tree

1 file changed

+116
-1
lines changed

1 file changed

+116
-1
lines changed

java-lambda/README.md

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Using Lambda to Implement Funtional Interface Method
44
- We have a *virtual* `Button` that when clicked will call an abstract method **onClick()** of a listener interface `OnClickListener`.
55
- We need to implement the **onClick()** so that it prints the `Button` name.
66

7-
87
`OnClickListener.java`
98

109
```java
@@ -34,3 +33,119 @@ public class Button {
3433
}
3534
```
3635

36+
- We have three ways to achieve that:
37+
1. **Implement** `OnClickListener` and override **onClick()** method.
38+
2. Use `OnClickListener` as an anonymous class.
39+
3. Use Lambda.
40+
41+
- Our main class is `App.java`
42+
43+
```java
44+
package com.hmkcode;
45+
46+
public class App
47+
{
48+
public static void main( String[] args ){
49+
System.out.println( "Running App..." );
50+
new App().run();
51+
}
52+
53+
public void run(){
54+
55+
Button myButton = new Button();
56+
myButton.setName("MyButton");
57+
58+
// 1. implements onClickListener
59+
60+
// 2. anonymous class
61+
62+
// 3. lambda
63+
64+
65+
// click the button
66+
myButton.click();
67+
}
68+
}
69+
```
70+
71+
### 1. **Implement** `OnClickListener` and override **onClick()** method
72+
73+
```java
74+
public class App implements OnClickListener
75+
{
76+
public static void main( String[] args ){...}
77+
78+
public void run(){
79+
80+
Button myButton = new Button();
81+
myButton.setName("MyButton");
82+
83+
// 1. implements onClickListener
84+
myButton.setOnClickListener(this);
85+
86+
// click the button
87+
myButton.click();
88+
}
89+
90+
@Override
91+
public void onClick(Button button) {
92+
System.out.println(button.getName() +" Clicked! - implements interface");
93+
94+
}
95+
}
96+
```
97+
98+
### 2. Use `OnClickListener` as an anonymous class
99+
100+
```java
101+
public class App
102+
{
103+
public static void main( String[] args ){...}
104+
105+
public void run(){
106+
107+
Button myButton = new Button();
108+
myButton.setName("MyButton");
109+
110+
// 2. anonymous class
111+
myButton.setOnClickListener(new OnClickListener() {
112+
@Override
113+
public void onClick(Button button) {
114+
System.out.println(button.getName() +" Clicked! - anonymous class");
115+
}
116+
});
117+
118+
// click the button
119+
myButton.click();
120+
}
121+
}
122+
```
123+
124+
### 3. Use Lambda
125+
126+
```java
127+
public class App
128+
{
129+
public static void main( String[] args ){...}
130+
131+
public void run(){
132+
133+
Button myButton = new Button();
134+
myButton.setName("MyButton");
135+
136+
// 3. lambda
137+
OnClickListener lambda = button -> { System.out.println(button.getName()+" Clicked! - lambda"); } ;
138+
myButton.setOnClickListener(lambda);
139+
140+
// click the button
141+
myButton.click();
142+
}
143+
}
144+
```
145+
146+
*To run the code use*
147+
148+
```
149+
java-lambda>mvn exec:java
150+
```
151+

0 commit comments

Comments
 (0)