> Programming > Java > Object Oriented Drum Kit: An OOP Class Example | ||||||
Object Oriented Drum Kit: An OOP Class ExampleThis class creates a simple drum rhythm. Object oriented programming involves many other features than instantiation. To get more of an idea get a good Java book and read up on it. This is what the class sounds like when run: (Exactly the same as the Static Kit.)
Let's have a closer look.
The class starts in the normal way by importing, class declaration, and creating the main() method. This is the entire main method!! All it does it create a new version of (instantiate) the OOP_Kit() class - this class:
This OOP_Kit() method does all the work done by the main() method in the previous example. The program is the same but the Java structure to achieve it is different. This method is called the constructor method. It has several unique features. firstly, its name is the same as a the class name. Secondly, it has no return type, not even void (which means no returned value). These are unique features of constructors.
The next method in this class is the KickPattern() method. This is identical to the previous example, but the method is NOT static because we want it instantiate it with the rest of the class. The only other feature to look out for is that when the Phrase is declared in this method it is given a start time of 0.0, the phrase declared above had none because we knew it would be overwritten with this phrase. Each of the snarePattern and hihatPattern methods look very similar to this one, so we won't show them here. Remember that the EndPattern() method is different in two ways. Firstly, it uses a CPhrase rather than a Phrase so that we can conveniently have have two drums sounding together. Secondly, the CPhrase is declared with no startTime argument. This means that when it is added to the Part it will be placed at the end of the other phrases. This is also convenient because it allows us to make the earlier patterns of any length or have any number of loops and the CPhrase will always be placed at the end. |
||||||