Dlaczego skoro istnieje już zapis: public Kobieta partnerka, musimy dodać: mezczyzna.partnerka = kobieta ? Co to zmienia w programie?
Co dokładnie wnoszą te zapisy, o które pytam?
Dyskutowane
Komentarze (4)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Guadalupe Gagnon
27 grudnia 2022, 16:29przydatny
A class is just a blueprint for an object, it is not an object itself. Once a class is defined you can create as many objects of that class as you see fit. Class fields, such as the "partnerka" variable in the Male and Female class, are unique to each individual object. Also, the partner variable in the class code is not initialized so when a new object is created the default value will be null. If you want a specific value you need to initialize it. For example: if you created 2 Male objects you could set each partner to a different value.
+1
Guadalupe Gagnon
27 grudnia 2022, 16:36przydatny
Sometimes it is easier to explain with a code example:
+2
Guadalupe Gagnon
27 grudnia 2022, 16:36przydatny
+1
Guadalupe Gagnon
27 grudnia 2022, 16:39przydatny
The code in the Main method is what is important (which is the first code post). Copy and paste this into your IDE and run it to see the output; then follow the code in Main line by line with the output.
+1