A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© iBadboy 中级黑马   /  2013-7-25 08:15  /  2323 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 iBadboy 于 2013-7-25 21:38 编辑

定义一个乐器(Instrument)接口,其中有抽象方法
   void play();
在InstrumentTest类中,定义一个方法
   void playInstrument(Instrument ins);
   并在该类的main方法中调用该方法。
要求:分别使用下列内部类完成此题。
成员内部类
局部内部类
匿名类我写的成员内部类:
  1. interface Instrument
  2. {
  3.         public void play();
  4. }
  5. class  InstrumentTest
  6. {
  7.         static class Instrument1 implements Instrument
  8.         {
  9.         public void play()
  10.                 {
  11.                         System.out.println("开始演奏");
  12.                 }
  13.         }
  14.         void playInstrument(Instrument ins)//这里可以带参
  15.         {
  16.                 ins.play();
  17.         }
  18.         public static void main(String[] args)
  19.         {
  20.                 InstrumentTest i=new InstrumentTest();
  21.                 i.playInstrument(new Instrument1());
  22.         }
  23. }
复制代码
局部内部类:
  1. interface Instrument
  2. {
  3.         public void play();
  4. }
  5. class  InstrumentTest
  6. {
  7.         
  8.         void playInstrument()//这里带参的话要传什么?还是不用带?
  9.         {
  10.                 class Instrument1 implements Instrument
  11.         {
  12.         public void play()
  13.                 {
  14.                         System.out.println("开始演奏");
  15.                 }
  16.         }
  17.                  new Instrument1().play();
  18.         }
  19.         public static void main(String[] args)
  20.         {
  21.                 InstrumentTest i=new InstrumentTest();
  22.                 i.playInstrument();
  23.         }
  24. }
复制代码
匿名内部类:
  1. interface Instrument
  2. {
  3.         public void play();
  4. }
  5. class  InstrumentTest
  6. {
  7.         
  8.         void playInstrument()//这里带参数的话怎么写?还是不用就行?
  9.         {
  10.                 new Instrument()
  11.                         {
  12.                         public void play()
  13.                     {
  14.                                 System.out.println("开始演奏");
  15.                     }
  16.                         }.play();
  17.         }
  18.         public static void main(String[] args)
  19.         {
  20.                 InstrumentTest i=new InstrumentTest();
  21.                 i.playInstrument();
  22.         }
  23. }
复制代码
我这样写对吗?题目要求中定义一个带参数的方法 void playInstrument(Instrument ins);,可是只有第一个可以带参,下面两个带上参数,在这里i.playInstrument();不知道该传什么?

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

6 个回复

倒序浏览
匿名内部类倒是可以用
playInstrument(new Instrument()
{
    public void play(){}
});
实现,局部的就不知道了 或许可以定义一个获得默认乐器的方法,通过这个方法得到这个默认的乐器再传给playInstrument做参数。不知道对不

评分

参与人数 1技术分 +1 收起 理由
杜光 + 1 每天提问并回答问题,是对知识的复习和积累.

查看全部评分

回复 使用道具 举报
草貌路飞 发表于 2013-7-25 11:41
匿名内部类倒是可以用
playInstrument(new Instrument()
{

Instrument是一个接口,没有实现它可以创建new Instrument()对象吗?加上这个就总是提示非法的类型开始。
回复 使用道具 举报
匿名内部类呀  不是直接new一个Instrument的对象,而是匿名的给它创建了一个子类对象
回复 使用道具 举报
草貌路飞 发表于 2013-7-25 19:38
匿名内部类呀  不是直接new一个Instrument的对象,而是匿名的给它创建了一个子类对象 ...

多谢啦,亲。。,我再试试看
回复 使用道具 举报
本帖最后由 xscn 于 2013-7-25 21:14 编辑
  1.     interface Instrument
  2.     {
  3.              void play();
  4.     }
  5.     class  InstrumentTest
  6.     {
  7.             
  8.             void playInstrument(Instrument ins)
  9.     {        ins.play();
  10.                        }
  11.             public static void main(String[] args)
  12.             {
  13.                     InstrumentTest i=new InstrumentTest();
  14.                     i.playInstrument(new Instrument()
  15.                                             {
  16.                                             public void play(){
  17.                         System.out.println("开始演奏aa");}}
  18.                                             );
  19.             }
  20.     }
复制代码
你的意思是不是这样?其实就是多态嘛,父类引用Instrument in指向子类对象,只不过换成匿名对象,不过不推荐这样写,代码太蛋疼了
回复 使用道具 举报
xscn 发表于 2013-7-25 21:11
你的意思是不是这样?其实就是多态嘛,父类引用Instrument in指向子类对象,只不过换成匿名对象,不过不推 ...

确实有点蛋疼,不过给力啊。。。哈哈,真得多谢啦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马