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
426ff999
Commit
426ff999
authored
Sep 11, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bloki inicjalizacyjne
parent
c85def11
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
4 deletions
+21
-4
Osoba.java
src/main/java/p05_klasy_wstep/Osoba.java
+18
-3
Program2_Tworzenie.java
src/main/java/p05_klasy_wstep/Program2_Tworzenie.java
+3
-1
No files found.
src/main/java/p05_klasy_wstep/Osoba.java
View file @
426ff999
...
...
@@ -19,22 +19,33 @@ public class Osoba {
// w pamięci istnieje jeden egzemplarz tej zmiennej
static
final
int
WIEK_PELNOLETNIOSCI
=
18
;
static
int
liczbaObiektow
=
0
;
private
static
int
liczbaObiektow
=
0
;
// konstruktor to taka specjalna metoda, która jest wykonywana
// podczas tworzenia nowego obiektu
Osoba
()
{
liczbaObiektow
++;
}
Osoba
(
String
imie
,
String
nazwisko
,
int
wiek
)
{
liczbaObiektow
++;
this
.
imie
=
imie
;
this
.
nazwisko
=
nazwisko
;
this
.
wiek
=
wiek
;
}
// blok inicjalizacyjny to fragment kodu wykonywany podczas
// tworzenia każdego obiektu jeszcze przed konstruktorem
{
System
.
out
.
println
(
"init Osoba"
);
liczbaObiektow
++;
}
// statyczny blok inicjalizacyjny jest wykonywany raz dla danej klasy
// gdy ta klasa jest ładowana do JVM
static
{
System
.
out
.
println
(
"static Osoba"
);
}
// metoda instancyjna (czyli bez słowa static) będzie uruchamiana na konkretnych obiektach
// i ma dostęp do pól tego obiektu
void
przedstawSie
()
{
...
...
@@ -53,4 +64,8 @@ public class Osoba {
return
wiek
>=
WIEK_PELNOLETNIOSCI
;
}
static
int
liczbaObiektow
()
{
return
liczbaObiektow
;
}
}
src/main/java/p05_klasy_wstep/Program2_Tworzenie.java
View file @
426ff999
...
...
@@ -3,6 +3,8 @@ package p05_klasy_wstep;
public
class
Program2_Tworzenie
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek programu"
);
System
.
out
.
println
(
"Osoba pełnoletnia ma "
+
Osoba
.
WIEK_PELNOLETNIOSCI
+
" lat."
);
// Tworząc obiekt "zainicjowany nullami" i dopiero później wpisując wartości pól
Osoba
a
=
new
Osoba
();
a
.
imie
=
"Ala"
;
...
...
@@ -17,6 +19,6 @@ public class Program2_Tworzenie {
Osoba
b
=
new
Osoba
(
"Ola"
,
"Malinowska"
,
40
);
b
.
przedstawSie
();
System
.
out
.
println
(
"Liczba obiektów: "
+
Osoba
.
liczbaObiektow
);
System
.
out
.
println
(
"Liczba obiektów: "
+
Osoba
.
liczbaObiektow
()
);
}
}
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