java类的多态编程。 (1)乐器(Instrument)分为:钢琴(Piano)、小提琴(Vio
public class Instrument {
创新新互联,凭借十多年的成都网站设计、成都做网站、外贸网站建设经验,本着真心·诚心服务的企业理念服务于成都中小企业设计网站有上1000+案例。做网站建设,选成都创新互联。
public void play(){
System.out.println("演奏乐器......");
}
}
public class Piano extends Instrument{
public void play(){
System.out.println("弹奏钢琴......");
}
}
public class Violin extends Instrument{
public void play(){
System.out.println("演奏小提琴......");
}
}
public class TestPlay {
public static void main(String[] args) {
Instrument pr = new Instrument();
pr.play();
Instrument p = new Piano();
p.play();
Instrument v = new Violin();
v.play();
}
}
java三种乐器弹奏方法不同……源代码
/**
* 歌曲类
*/
public class Music {
public Music() {
super();
}
public Music(String song) {
super();
System.out.println("《"+song+"》");
System.out.println(".....多来米发所拉稀....");
}
}
/**
* 乐器接口,凡实现该接口的都游演奏的功能
*/
public interface Instrument {
public Music musical(String song);
}
/**
* 管弦类乐器
*/
public class Orchestral implements Instrument {
public Music musical(String song) {
System.out.println("下面用管弦类乐器演奏 "+song);
return new Music(song);
}
}
/**
* 弹奏类乐器
*/
public class Spiccato implements Instrument {
public Music musical(String song) {
System.out.println("下面用弹奏类乐器弹奏 "+song);
return new Music(song);
}
}
/**
* 其他类型乐器
*/
public class Other implements Instrument {
String instrument;
public Other() {
super();
}
public Other(String instrument) {
this.instrument = instrument;
}
public Music musical(String song) {
System.out.println("用 "+instrument+" 演奏的 "+song);
return new Music(song);
}
public static void main(String[] args) {
Other other = new Other("古筝");
other.musical("高山流水");
}
}
/**
* 笛子
*/
public class Fife extends Orchestral {
public Fife(String song) {
super.musical(song);
System.out.println("这是用横笛吹奏的 "+song);
}
}
/**
* 萨克斯
*/
public class Sax extends Orchestral {
public Sax(String song) {
super.musical(song);
System.out.println("这是用萨克斯演奏的 "+song);
}
}
/**
* 吉他
*/
public class Guitar extends Spiccato {
public Guitar(String song) {
super.musical(song);
System.out.println("这是吉他弹奏的");
}
}
/**
* 钢琴
*/
public class Piano extends Spiccato {
public Piano() {
super();
}
public Piano(String song) {
super.musical(song);
System.out.println("这是在用钢琴弹奏 "+song);
}
}
教我写下小代码吧 java
interface Instrument{
void play();
}
class Piano implements Instrument{
public void play() {
System.out.println("play Piano");
}
}
class Violin implements Instrument{
public void play() {
System.out.println("play Violin");
}
}
public class InstrumentTest{
public static void main(String[] args) {
Instrument i1 = new Piano();
Instrument i2 = new Violin();
i1.play();
i2.play();
}
}
当前标题:钢琴java代码 钢琴程序编程
标题URL:http://scgulin.cn/article/dodsiso.html