扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
/**
10年的惠民网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整惠民建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“惠民网站设计”,“惠民网站推广”以来,每个客户项目都认真落实执行。
* 歌曲类
*/
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);
}
}
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;
//无参构造
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();
}
}
/*根据你的需求来看 用向上转型
* 一个对象可以看做是本类类型,也可以看做它的超类类型。
* 取得一个对象的引用并将它看做超类的对象,成为向上转型。
* */
//创建抽象的音乐Instrument类,在该类定义了一个play()弹奏方法,并创建两个子类:
//钢琴和小提琴。在InstrumentTest类中定义testPlay()的演奏方法,
//该方法接受音乐类的对象做方法的参数,并调用参数的play()方法进行演奏
//创建一个类名为InstrumentTest.java源文件,只在一个文件内,内容如下。
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)演奏
}
}
当然这个抽象的音乐类 也可以改成接口。
希望能帮到您,也希望你能满意!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流