Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jvstd1
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
jvstd1
Commits
a7383c30
Commit
a7383c30
authored
Oct 16, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lambdy - ostatnie dodatki
parent
ab6def50
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
3 deletions
+42
-3
FunkcjaLiczbowa.java
src/main/java/p17_lambdy/FunkcjaLiczbowa.java
+22
-2
MojProgram.java
src/main/java/p17_lambdy/MojProgram.java
+6
-0
Program.java
src/main/java/p17_lambdy/Program.java
+14
-1
No files found.
src/main/java/p17_lambdy/FunkcjaLiczbowa.java
View file @
a7383c30
package
p17_lambdy
;
package
p17_lambdy
;
/* Interfejs funkcyjny to taki, który ma jedną metodę a
n
strakcyjną
/* Interfejs funkcyjny to taki, który ma jedną metodę a
b
strakcyjną
(czyli pozbawioną implementacji).
(czyli pozbawioną implementacji).
Można jednak definiować metody z domyślną implementacją (oznaczone default).
Można też definiować metody statyczne.
Wyrażenia lambda, które są zgodne z nagłówkiem tej metody,
Wyrażenia lambda, które są zgodne z nagłówkiem tej metody,
mogą służyć do tworzenia obiektów zgodnych z tym interfejsem.
mogą służyć do tworzenia obiektów zgodnych z tym interfejsem.
*/
Przed interfejsem funkcyjnym MOŻEMY, ale nie musimy, wpisać adnotację @FunctionalInterface
*/
@FunctionalInterface
public
interface
FunkcjaLiczbowa
{
public
interface
FunkcjaLiczbowa
{
double
oblicz
(
double
arg
);
double
oblicz
(
double
arg
);
default
int
obliczJakoInt
(
double
arg
)
{
return
(
int
)
oblicz
(
arg
);
}
static
FunkcjaLiczbowa
identycznosc
()
{
return
x
->
x
;
}
static
double
nicniezmieniaj
(
double
x
)
{
return
x
;
}
static
FunkcjaLiczbowa
constant
(
double
value
)
{
return
x
->
value
;
}
}
}
src/main/java/p17_lambdy/MojProgram.java
View file @
a7383c30
...
@@ -13,5 +13,11 @@ public class MojProgram {
...
@@ -13,5 +13,11 @@ public class MojProgram {
System
.
out
.
println
(
Arrays
.
toString
(
a
));
System
.
out
.
println
(
Arrays
.
toString
(
a
));
MojFrameworkFunkcyjny
.
zastosujDoTablicy
(
a
,
x
->
x
*
x
);
MojFrameworkFunkcyjny
.
zastosujDoTablicy
(
a
,
x
->
x
*
x
);
System
.
out
.
println
(
Arrays
.
toString
(
a
));
System
.
out
.
println
(
Arrays
.
toString
(
a
));
MojFrameworkFunkcyjny
.
zastosujDoTablicy
(
a
,
FunkcjaLiczbowa
.
identycznosc
());
System
.
out
.
println
(
Arrays
.
toString
(
a
));
MojFrameworkFunkcyjny
.
zastosujDoTablicy
(
a
,
FunkcjaLiczbowa:
:
nicniezmieniaj
);
System
.
out
.
println
(
Arrays
.
toString
(
a
));
MojFrameworkFunkcyjny
.
zastosujDoTablicy
(
a
,
FunkcjaLiczbowa
.
constant
(
5
));
System
.
out
.
println
(
Arrays
.
toString
(
a
));
}
}
}
}
src/main/java/p17_lambdy/Program.java
View file @
a7383c30
...
@@ -12,14 +12,27 @@ public class Program {
...
@@ -12,14 +12,27 @@ public class Program {
System
.
out
.
println
(
f3
.
oblicz
(
7
));
System
.
out
.
println
(
f3
.
oblicz
(
7
));
System
.
out
.
println
(
"Obiekt f3: "
+
f3
);
System
.
out
.
println
(
"Obiekt f3: "
+
f3
);
System
.
out
.
println
(
"Klasa obiektu: "
+
f3
.
getClass
().
getName
());
System
.
out
.
println
(
"Klasa obiektu: "
+
f3
.
getClass
().
getName
());
if
(
f3
instanceof
FunkcjaLiczbowa
)
{
// skoro lambda lest obiektem, to mogę wpisać do zmiennej typu Object
Object
o
=
f3
;
// System.out.println(o.oblicz(8));
if
(
o
instanceof
FunkcjaLiczbowa
)
{
System
.
out
.
println
(
"Tak, to jest ten typ."
);
System
.
out
.
println
(
"Tak, to jest ten typ."
);
}
}
// Jednak nie mogę wpisać lambdy bezpośrednio do zmiennej o
// bo kompilator nie wie w tym miejscu, do jakiego interfejsu ma pasować ta lambda.
// o = y->y-1;
// Aby użyć wyrażenia lambda, z kontekstu musi wynikać, jakiego typu oczekujemy.
o
=
(
FunkcjaLiczbowa
)
y
->
y
-
1
;
System
.
out
.
println
();
System
.
out
.
println
();
FunkcjaLiczbowa
f4
=
x
->
10
*
x
;
FunkcjaLiczbowa
f4
=
x
->
10
*
x
;
FunkcjaLiczbowa
f5
=
Math:
:
sqrt
;
FunkcjaLiczbowa
f5
=
Math:
:
sqrt
;
System
.
out
.
println
(
f4
.
oblicz
(
9
));
System
.
out
.
println
(
f4
.
oblicz
(
9
));
System
.
out
.
println
(
f5
.
oblicz
(
16
));
System
.
out
.
println
(
f5
.
oblicz
(
16
));
System
.
out
.
println
(
f5
.
oblicz
(
11
));
System
.
out
.
println
(
f5
.
obliczJakoInt
(
11
));
}
}
}
}
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