「乐器演奏JAVA」乐器演奏家
本篇文章给大家谈谈乐器演奏JAVA,以及乐器演奏家对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java三种乐器弹奏方法不同……源代码
- 2、用java新建一个乐器类,包含属性:名称、重量、品牌、价格;包含方法:不带参数的构造方法、表演方法
- 3、java类的多态编程。 (1)乐器(Instrument)分为:钢琴(Piano)、小提琴(Vio
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新建一个乐器类,包含属性:名称、重量、品牌、价格;包含方法:不带参数的构造方法、表演方法
/**
* 乐器类
*/
public class Instrument {
//名称
private String name;
//重量
private String weight;
//品牌
private String brand;
//价格
private String price;
//无参构造
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;
//出产国
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();
}
}
java类的多态编程。 (1)乐器(Instrument)分为:钢琴(Piano)、小提琴(Vio
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();
}
}
乐器演奏JAVA的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于乐器演奏家、乐器演奏JAVA的信息别忘了在本站进行查找喔。
发布于:2022-12-12,除非注明,否则均为
原创文章,转载请注明出处。