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
512b23f1
Commit
512b23f1
authored
Sep 11, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pierwsze metody
parent
3fcfc39f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
0 deletions
+41
-0
Osoba.java
src/main/java/p05_klasy_wstep/Osoba.java
+23
-0
Program1.java
src/main/java/p05_klasy_wstep/Program1.java
+18
-0
No files found.
src/main/java/p05_klasy_wstep/Osoba.java
View file @
512b23f1
...
...
@@ -14,4 +14,27 @@ public class Osoba {
String
imie
,
nazwisko
;
int
wiek
;
// jeśli przed zmienną jest słowo static, to taka zmienna należy do klasy,
// a nie do poszczególnych obiektów
// w pamięci istnieje jeden egzemplarz tej zmiennej
static
final
int
WIEK_PELNOLETNIOSCI
=
18
;
// metoda instancyjna (czyli bez słowa static) będzie uruchamiana na konkretnych obiektach
// i ma dostęp do pól tego obiektu
void
przedstawSie
()
{
System
.
out
.
println
(
"Nazywam się "
+
imie
+
" "
+
nazwisko
+
" i mam "
+
wiek
+
" lat."
);
// można też tak, ale w Javie nie ma takiego obowiązku:
// System.out.println("Nazywam się " + this.imie + " " + this.nazwisko + " i mam " + this.wiek + " lat.");
}
int
liczbaZnakow
()
{
int
imieLength
=
imie
==
null
?
0
:
imie
.
length
();
int
nazwiskoLength
=
nazwisko
==
null
?
0
:
nazwisko
.
length
();
return
imieLength
+
nazwiskoLength
;
}
boolean
pelnoletnia
()
{
return
wiek
>=
WIEK_PELNOLETNIOSCI
;
}
}
src/main/java/p05_klasy_wstep/Program1.java
View file @
512b23f1
...
...
@@ -23,6 +23,24 @@ public class Program1 {
System
.
out
.
println
(
"pola a: "
+
a
.
imie
+
" "
+
a
.
nazwisko
+
" "
+
a
.
wiek
);
System
.
out
.
println
(
"pola b: "
+
b
.
imie
+
" "
+
b
.
nazwisko
+
" "
+
b
.
wiek
);
System
.
out
.
println
();
// odwołanie do zmiennej statycznej - zalecany jest dostęp poprzez nazwę klasy:
System
.
out
.
println
(
Osoba
.
WIEK_PELNOLETNIOSCI
);
// zadziałą też, ale jest niezalecany - dostęp poprzez obiekty:
System
.
out
.
println
(
a
.
WIEK_PELNOLETNIOSCI
);
System
.
out
.
println
();
a
.
przedstawSie
();
b
.
przedstawSie
();
System
.
out
.
println
(
"długość imienia i nazwiska a: "
+
a
.
liczbaZnakow
());
System
.
out
.
println
(
a
.
imie
+
(
a
.
pelnoletnia
()
?
" jest "
:
" nie jest "
)
+
"osobą pełnoletnią."
);
if
(
b
.
pelnoletnia
())
{
System
.
out
.
println
(
b
.
imie
+
" może kupić piwo"
);
}
}
}
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