/**
10年的惠民網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整惠民建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“惠民網(wǎng)站設(shè)計”,“惠民網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
* 歌曲類
*/
public class Music {
public Music() {
super();
}
public Music(String song) {
super();
System.out.println("《"+song+"》");
System.out.println(".....多來米發(fā)所拉稀....");
}
}
/**
* 樂器接口,凡實現(xiàn)該接口的都游演奏的功能
*/
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);
}
}
public class Instrument {
public void play(){
System.out.println("instrument is playing");
}
}
public class Piano extends Instrument{
public void play(){
System.out.println("piano is playing");
}
}
public class Violin extends Instrument{
public void play(){
System.out.println("voilin is playing");
}
}
測試:
public class InstrumentTest(){
public static void main(String[] args) {
Instrument instrument = new Piano();
instrument.play();
instrument = new Violin();
instrument.play();
}
}
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();
}
}
public class Instrument {???
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();
}
}
/**
*?樂器類
*/
public?class?Instrument?{
//名稱
private?String?name;
//重量
private?String?weight;
//品牌
private?String?brand;
//價格
private?String?price;
//無參構(gòu)造
public?Instrument()?{
}
//表演方法
public?void?perform()?{
System.out.println("表演方法");
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getWeight()?{
return?weight;
}
public?void?setWeight(String?weight)?{
this.weight?=?weight;
}
public?String?getBrand()?{
return?brand;
}
public?void?setBrand(String?brand)?{
this.brand?=?brand;
}
public?String?getPrice()?{
return?price;
}
public?void?setPrice(String?price)?{
this.price?=?price;
}
}
/**
*?鋼琴類
*/
public?class?Piano?extends?Instrument{
//類型
private?String?type;
//制作年份
private?String?year;
//出產(chǎn)國
private?String?country;
//重寫表演方法
public?void?perform()?{
System.out.println("用手指輕輕敲擊彈奏");
}
public?String?getType()?{
return?type;
}
public?void?setType(String?type)?{
this.type?=?type;
}
public?String?getYear()?{
return?year;
}
public?void?setYear(String?year)?{
this.year?=?year;
}
public?String?getCountry()?{
return?country;
}
public?void?setCountry(String?country)?{
this.country?=?country;
}
}
/**
*?小提琴類
*/
public?class?Violin?extends?Instrument{
//琴弦廠家
private?String?factory;
//制作者
private?String?maker;
//重寫表演方法
public?void?perform()?{
System.out.println("用琴弓輕輕拉動琴弦");
}
public?String?getFactory()?{
return?factory;
}
public?void?setFactory(String?factory)?{
this.factory?=?factory;
}
public?String?getMaker()?{
return?maker;
}
public?void?setMaker(String?maker)?{
this.maker?=?maker;
}
}
/**
*?測試類
*/
public?class?Test?{
public?static?void?main(String[]?args)?{
//鋼琴表演
Piano?p?=?new?Piano();
p.perform();
//小提琴表演
Violin?v?=?new?Violin();
v.perform();
}
}
/*根據(jù)你的需求來看 用向上轉(zhuǎn)型
* 一個對象可以看做是本類類型,也可以看做它的超類類型。
* 取得一個對象的引用并將它看做超類的對象,成為向上轉(zhuǎn)型。
* */
//創(chuàng)建抽象的音樂Instrument類,在該類定義了一個play()彈奏方法,并創(chuàng)建兩個子類:
//鋼琴和小提琴。在InstrumentTest類中定義testPlay()的演奏方法,
//該方法接受音樂類的對象做方法的參數(shù),并調(diào)用參數(shù)的play()方法進行演奏
//創(chuàng)建一個類名為InstrumentTest.java源文件,只在一個文件內(nèi),內(nèi)容如下。
abstract class Instrument {
public abstract void play();//彈奏方法
}
class Piano extends Instrument{
public void play(){ //鋼琴(Piano)演奏
System.out.println("鋼琴(Piano)演奏");
}
}
class Violin extends Instrument{
public void play(){ //小提琴(Violin)演奏
System.out.println("小提琴(Violin)演奏");
}
}
public class InstrumentTest{
public void testPlay(Instrument instrument){
instrument.play(); //傳遞音樂對象的不同,進行不同的演奏
}
public static void main(String[] args) {
InstrumentTest it = new InstrumentTest();
Piano piano = new Piano(); //鋼琴
Violin violin = new Violin();//小提琴
it.testPlay(piano); //鋼琴(Piano)演奏
it.testPlay(violin); //小提琴(Violin)演奏
}
}
當然這個抽象的音樂類 也可以改成接口。
希望能幫到您,也希望你能滿意!
新聞名稱:java電子鋼琴代碼 c語言電子鋼琴
URL地址:http://jinyejixie.com/article46/dosooeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、域名注冊、網(wǎng)站內(nèi)鏈、搜索引擎優(yōu)化、軟件開發(fā)、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)