Note.java an enumeration class
Package Lesson8polymorphism.music;public enum Note {middle_c, C_sharp, B_flat;}
Music5.java
/*** book: "Thinking In Java" * Features: Interface * File: music.java* time: April 2, 2015 16:50:48* Author: cutter_point*/package Lesson9interfaces.music5;import Java.util.scanner;import lesson8polymorphism.music.note;import Static Net.mindview.util.print.*;interface Instrument{int VALUES = 5;//default is static and final void Paly (Note n);//The method cannot be implemented void adjust ();} Class Wind implements instrument//must implement all methods of the interface {@Overridepublic void paly (Note n) {print (this + ". Play ()" + N);} @Overridepublic void adjust () {print (this + ". Adjust ()"); Public String toString () {return ' wind () ';}} Class Percussion implements instrument{@Overridepublic void Paly (Note n) {print (this + ". Play ()" + N);} @Overridepublic void adjust () {print (this + ". Adjust ()"); Public String toString () {return ' percussion () ';}} Class stringed implements instrument{@Overridepublic void Paly (Note n) {print (this + ". Play ()" + N);} @Overridepublic void adjust () {print (this + ". Adjust ()"); Public String toString () {return ' stringed () ';}} Class Brass extends Wind{public String tostring () {return "Brass";}} Class Woodwind extends Wind{public String toString () {return ' woodwind ';}} public class Music5 {static void tune (instrument i) {i.paly (Note.middle_c);//middle_c, C_sharp, B_flat;} static void Tuneall (instrument[] e) {//Remove each of the calls to the tune function for (instrument i:e) {tune (i);}} public static void Main (String [] args) {//scanner s = new Scanner (system.in);//system.out.println (S.next ()); instrument[ ] Orchestra = {new Wind (), new percussion (), New stringed (), new Brass (), new woodwind ()};tuneall (orchestra);}}
Output Result:
Wind (). Play () Middle_c obj1
Percussion (). Play () Middle_c obj1
Stringed (). Play () Middle_c obj1
Brass.play () Middle_c obj1
Woodwind.play () Middle_c obj1
"Thinkinginjava" 10, interface use