Hello,
I am dealing with chapter 5 at the moment and the concept of constructor popped up some questions.
As suggested in the chapter, I added all the keys using a string array/loop via the world class.
It all works fine. Now, I'd like to understand why :-)
In the classes for both white and black keys, I used the following variables/ constructor (here's the one for my white keys in the class called Key)
What I don't quite understand in this concept is the following:
- How does the system "know" that when I press i.e. the a-key this very key on the piano is pressed and the respective sound must be played? I understand that using the string array/loop a respective key-/sound-combination is created; but how can the key class "know" which key is currently pressed? I mean, I don't see the connection between the world class and the key class
- Why do I need four string variables (key, keyName, sound, soundfile) to make it work? Why isn't key/sound enough?
- Why do I need to create the variables keyName/soundFile in the constructor itself and not in the same place like key and sound? And why do I have to create keyName/soundFile in the "( )"?
Thanks for your help in advance!
1 2 3 4 5 6 7 8 9 | private boolean isDown; private String key; private String sound; public Key (String keyName, String soundFile) { key = keyName; sound = soundFile; } |