java继承

来源:互联网 发布:yum x window system 编辑:程序博客网 时间:2024/04/27 13:50
package com.homework.ch2;public class Instrument {public void play(){}}package com.homework.ch2;public class Piano extends Instrument {    public void play(){System.out.println("play the piano!");    }}package com.homework.ch2;public class Violin extends Instrument {@Overridepublic void play() {// TODO Auto-generated method stubSystem.out.println("play the vioin!");}}package com.homework.ch2;import java.util.List;import java.util.ArrayList;public class InstrumentTest {List<Instrument> instruments = new ArrayList<Instrument>();/*** @param args*/public static void main(String[] args) {//Instrument instrument=new Piano();Instrument instrument=new Violin();testPlay(instrument);}public InstrumentTest(List<Instrument> instruments) {super();this.instruments = instruments;}public static void testPlay(Instrument instrument){instrument.play();}}


0 0
原创粉丝点击