import java.util.*;abstract class Instrument4 {
  int i; // storage allocated for each
  public abstract void play();
  public String what() {
    return "Instrument4";
  }
  public abstract void adjust();
}class Wind4 extends Instrument4 {
  public void play() {
    System.out.println("Wind4.play()");
  }
  public String what() { return "Wind4"; }
  public void adjust() {}
}class Percussion4 extends Instrument4 {
  public void play() {
    System.out.println("Percussion4.play()");
  }
  public String what() { return "Percussion4"; }
  public void adjust() {}
}class Stringed4 extends Instrument4 {
  public void play() {
    System.out.println("Stringed4.play()");
  }
  public String what() { return "Stringed4"; }
  public void adjust() {}
}class Brass4 extends Wind4 {
  public void play() {
    System.out.println("Brass4.play()");
  }
  public void adjust() { 
    System.out.println("Brass4.adjust()");
  }
}class Woodwind4 extends Wind4 {
  public void play() {
    System.out.println("Woodwind4.play()");
  }
  public String what() { return "Woodwind4"; }
}public class Music4 {
  // Doesn't care about type, so new types
  // added to the system still work right:
  static void tune(Instrument4 i) {
    // ...
    i.play();
  }
  static void tuneAll(Instrument4[] e) {
    for(int i = 0; i < e.length; i++)
      tune(e[i]);
  }
  public static void main(String[] args) {
    Instrument4[] orchestra = new Instrument4[5];
    int i = 0;
    // Upcasting during addition to the array:
    orchestra[i++] = new Wind4();
    orchestra[i++] = new Percussion4();
    orchestra[i++] = new Stringed4();
    orchestra[i++] = new Brass4();
    orchestra[i++] = new Woodwind4();
    tuneAll(orchestra);
  }
} ///:~
thinking in JAVA中的.
不是说用abatract就不能定义对象我吗?
static void tune(Instrument4 i)这是怎么回事?

解决方案 »

  1.   

    那个不叫对象,你可以把他理解为引用referrence 
    new Instrument4()才叫对象Object
      

  2.   


    tune(e[i]); 
    你传进的是e[i]
    比如说e[0]它代表的就是new   Wind4()
    这个e[0]就是一个子类的对象
    把它传到那个函数里也就相当于把那个引用指向了new   Wind4()
    即Instrument4 i = new Wind4();
    上面那句成立吧
      

  3.   

    抽象类虽然不能创建对象,但它允许继承,
    Instrument4 i  这里只是创建了一个引用(你可以理解为指针),
    用来指向其子类的对象,比如说: Wind4是其子类,那么
    Instrument4   i   =   new   Wind4(); 
    这句话表示把Instrument4 这个抽象类的子类对象的引用传给i注意:
        Instrument4   i   =   new   Wind4(); 这个语句
         里的i同样只是个引用,用来指向其子类对象.
      

  4.   

    说的通俗一点吧
    对象都是new 出来的。
    因为不能声明抽像类的对象
    所以new 后面不能跟抽像类。Instrument4       i //这里并没有声明对象。 只是声明了一个引用,这个引用是Instruments4类型的。
      

  5.   

    对象都是new   出来的。 
    ====================
    这句话有点太绝对了 
      

  6.   

    那这又是怎么回事啊
    public   static   void   main(String[]   args)   { 
            Instrument4[]   orchestra   =   new   Instrument4[5];
            int   i   =   0; 
            //   Upcasting   during   addition   to   the   array: 
            orchestra[i++]   =   new   Wind4(); 
            orchestra[i++]   =   new   Percussion4(); 
            orchestra[i++]   =   new   Stringed4(); 
            orchestra[i++]   =   new   Brass4(); 
            orchestra[i++]   =   new   Woodwind4(); 
            tuneAll(orchestra); 
        } 
      

  7.   

    呵呵 
    你看着new Instrument4[5];  像是在实例化那个abstract类 
    其实不是这样的 数组里的每个元素也都只是引用而已。
    这句话的意思是  声明五个Instrument4类型的引用。
    然后下面 
                    orchestra[i++]       =       new       Wind4();   
                    orchestra[i++]       =       new       Percussion4();   
                    orchestra[i++]       =       new       Stringed4();   
                    orchestra[i++]       =       new       Brass4();   
                    orchestra[i++]       =       new       Woodwind4();   
    这才是给每个引用赋值。 让它们指向不同的对象
    你可以试着把new       Wind4();   改成new  Instrument4();  就会得到错误信息的
    这下明白了吗?