Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20231104-KursPodstawowyALX
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
20231104-KursPodstawowyALX
Commits
0414eb54
Commit
0414eb54
authored
Jan 27, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pierwsze lambdy
parent
8ee56f9f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
0 deletions
+52
-0
Lambdy.java
src/main/java/p13_lambdy/Lambdy.java
+52
-0
No files found.
src/main/java/p13_lambdy/Lambdy.java
0 → 100644
View file @
0414eb54
package
p13_lambdy
;
public
class
Lambdy
{
public
static
void
main
(
String
[]
args
)
{
// Od Javy 8, aby sworzyć obiekt zgodny z interfejsem, można użyć wyrażenia lambda:
FunkcjaLiczbowa
f1
=
x
->
x
*
x
;
// f1 jest obiektem pewnej automatycznie tworzonej klasy, która implementuje interfejs.
System
.
out
.
println
(
f1
);
if
(
f1
instanceof
FunkcjaLiczbowa
)
{
System
.
out
.
println
(
"Potwierdzam - wszystko OK"
);
}
// na takim obiekcie można wywołać metodę z interfejsu:
System
.
out
.
println
(
f1
.
oblicz
(
5
));
// Aby można było zastosować wyrażenie lambda do danego interfejsu, to musi to być
// INTERFEJS FUNKCYJNY, czyli taki, że jest w nim tylko jedna metoda do zaimplementowania.
// Dzięki temu kompilator wie, że to, co zapisujemy za pomocą strzałek, jest implementacją właśnie tej metody.
// MouseListener nie jest funkcyjny, bo ma zadeklarowanych 6 metod
// java.awt.event.MouseListener ml = event -> {System.out.println("lalala");};
// ActionListener jest funkcyjny, bo ma zadeklarowaną 1 metodę
java
.
awt
.
event
.
ActionListener
al
=
event
->
{
System
.
out
.
println
(
"bum bum"
);};
// przykład nieco bardziej rozbudowanej lambdy, gdzie jest kilka kroków, a na końcu return.
FunkcjaLiczbowa
f2
=
(
double
argument
)
->
{
double
czescCalkowita
=
(
long
)
argument
;
double
czescUlamkowa
=
argument
%
1
;
return
10
*
czescCalkowita
+
czescUlamkowa
;
};
System
.
out
.
println
(
f2
.
oblicz
(
13
));
System
.
out
.
println
(
f2
.
oblicz
(
14.65
));
System
.
out
.
println
();
// Jeśli istnieje metoda, której nagłówek pasuje do naszego interfejsu, to zamiast używać lambdy:
FunkcjaLiczbowa
p1
=
x
->
Math
.
sqrt
(
x
);
// można bezpośrednio wskazać tę metodę ("method reference")
FunkcjaLiczbowa
p2
=
Math:
:
sqrt
;
System
.
out
.
println
(
p1
);
System
.
out
.
println
(
p2
);
System
.
out
.
println
(
p1
.
oblicz
(
100
));
System
.
out
.
println
(
p2
.
oblicz
(
256
));
System
.
out
.
println
(
p2
.
oblicz
(
2
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment