public class Solution {
public static void main(String[] args) {
Kot kot = new Kot();
Pies pies = new Pies();
Ryba ryba = new Ryba();
Kobieta kobieta = new Kobieta();
pies.wlasciciel = kobieta;
kot.wlasciciel = kobieta;
ryba.wlasciciel = kobieta;
Samo zadanie wykonałem dobrze i zostało mi ono zaliczone. Przy rozwiązywaniu kierowałem się pamięcią, że gdzieś już kiedyś widziałem w poprzednich zadaniach coś podobnego, no i zatrybiło. Jednak kod wklepałem bezmyślne, nie wiedząc tak naprawdę co dokładnie zrobiłem. O ile rozumiem przypisanie zmiennych zwierząt do kobiety jako właściciela, to nie za bardzo rozumiem jaką funkcję pełni kropka między zmiennymi zwierząt a zmienną wlasciciel - tj. np. ryba.wlasciciel = kobieta.
Prosiłbym o pomoc z objaśnieniem tego, co dokładnie zrobiliśmy, jaką funkcję pełni kropka między zmiennymi, a nawet może jakieś praktyczne zastosowanie danej metody? Gdybym miał teraz podać odpowiedź i wziąć to na logikę, to powiedziałbym że: dodajemy zmienną zwierzęcia, a następnie oddzielamy kropką drugą zmienną, co pozwala na przypisanie obu zmiennych na raz do zmiennej kobieta. Czy takie myślenie jest w porządku?
Jakieś objaśnienia przekonwertowane a chłopski rozum mile widziane.
Dziękuje z góry i pozdrawiam!
Jak to rozumieć?
Dyskutowane
Komentarze (2)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Guadalupe Gagnon
11 kwietnia 2022, 14:02przydatny
The dot operator allows you to access members of the class, meaning defined variables and methods. A common analogy used is that code that defines a class is like a blueprint. Let's say you have a blue print for a house you can copy that blue print and build as many houses that you want, and each house is its separate entity. The same thing occurs with class code and objects, the class code is the 'blueprint' and each object is its own entity (house). It's not a perfect analogy as a blueprint defines how to build something, where class code defines how an object of that class behaves, but it is helpful when you are first learning.
Defining behavior is accomplished with fields and methods. Fields are the contained variables such as the 'wlasciciel' variable in each of the pet classes in this task.
Using the 'Ryba' class as an example: That class definition states that it contains a variable called 'wlasciciel'. That variable is only useable with objects of that class (created with the 'new' variable), in this case the 'ryba' variable. To access the class variable 'wlasciciel' use the dot operator on the 'ryba' variable. The rule is the same no matter what the names of the variables.
+1
Guadalupe Gagnon
11 kwietnia 2022, 14:06
Here is a quick example:
See I just made a custom class with 2 fields ('example' and 'test') and one method ('doSomething()').
In the main() method I created 2 variables of that class called simply 'one' and 'two', with each being assigned its own object of that class with the 'new' keyword. If you remember that the class code is just a blueprint of objects, the created objects are independent of each other and can contain different data. I demonstrate that in the lines following the creation of the objects where you can see that the 'example' class variable is set to different values and then printed out to show that they are in fact saved as different values. I did the same for the 'test' class variable. 0