随机优雅solution

package polymorphism.music;

public enum Note {
	MIDDLE_C, C_SHARP, B_FLAT;
}

package polymorphism.music3;

import polymorphism.music.Note;

class Instrument {
	void play(Note n) { System.out.println("Instrument.play() " + n); }
	String what() { return "Instrument"; }
	void adjust() { System.out.println("Adjusting Instrument"); }
}

class Wind extends Instrument {
	void play(Note n) { System.out.println("Wind.play() " + n); }
	String what() { return "Wind"; }
	void adjust() { System.out.println("Adjusting Wind"); }
}

class Percussion extends Instrument {
	void play(Note n) { System.out.println("Percussion.play() " + n); }
	String what() { return "Percussion"; }
	void adjust() { System.out.println("Adjusting Percussion"); }
}

class Stringed extends Instrument {
	void play(Note n) { System.out.println("Stringed.play() " + n); }
	String what() { return "Stringed"; }
	void adjust() { System.out.println("Adjusting Stringed"); }
}

class Brass extends Wind {
	void play(Note n) { System.out.println("Brass.play() " + n); }
	void adjust() { System.out.println("Adjusting Brass"); }
}

class Woodwind extends Wind {
	void play(Note n) { System.out.println("Woodwind.play() " + n); }
	void adjust() { System.out.println("Adjusting Woodwind"); }
}

public class Music3 {

	public static void tune(Instrument i) {
		i.play(Note.MIDDLE_C);
	}
	
	public static void tuneAll(Instrument[] e) {
		for(Instrument i : e) {
			tune(i);
		}
	}
	
	public static void main(String[] args) {
		Instrument[] orchestra = {
			new Wind(),
			new Percussion(),
			new Stringed(),
			new Brass(),
			new Woodwind()
		};
		tuneAll(orchestra);
	}
}

Wind.play() MIDDLE_C
Percussion.play() MIDDLE_C
Stringed.play() MIDDLE_C
Brass.play() MIDDLE_C
Woodwind.play() MIDDLE_C

//: polymorphism/E06_MusicToString.java
/****************** Exercise 6 *****************
* Change Music3.java so what() becomes the root
* Object method toString(). Print the Instrument
* objects using System.out.println() (without
* any casting).
***********************************************/
package polymorphism;

import polymorphism.music.Note;

class Instrument {
	void play(Note n) { System.out.println("Instrument.play() " + n); }
	public String toString() { return "Instrument"; }
	void adjust() {}
}

class Wind extends Instrument {
	void play(Note n) { System.out.println("Wind.play() " + n); }
	public String toString() { return "Wind"; }
}

class Percussion extends Instrument {
	void play(Note n) { System.out.println("Percussion.play() " + n); }
	public String toString() { return "Percussion"; }
}

class Stringed extends Instrument {
	void play(Note n) { System.out.println("Stringed.play() " + n); }
	public String toString() { return "Stringed"; }
}

class Brass extends Wind {
	void play(Note n) { System.out.println("Brass.play() " + n); }
	void adjust() { System.out.println("Brass.adjust()"); }
}

class Woodwind extends Wind {
	void play(Note n) { System.out.println("Woodwind.play() " + n); }
	public String toString() { return "Woodwind"; }
}

public class E06_MusicToString {
	static Instrument[] orchestra = {
		new Wind(),
		new Percussion(),
		new Stringed(),
		new Brass(),
		new Woodwind()
	};
	public static void printAll(Instrument[] orch) {
		for(Instrument i : orch) {
			System.out.println(i);
		}
	}
	public static void main(String args[]) {
		printAll(orchestra);
	}
}

Wind
Percussion
Stringed
Wind
Woodwind



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值