上溯造型就是upcast,子类cast成父类,很朴素的思想吗!
就好比(human)boy一样!

解决方案 »

  1.   

    如上面所说 子类转换为父类,class Instrument {
      public void play() {}
      static void tune(Instrument i) {
        // ...
        i.play();
      }
    }// Wind objects are instruments
    // because they have the same interface:
    class Wind extends Instrument {
      public static void main(String[] args) {
        Wind flute = new Wind();
        Instrument.tune(flute); // Upcasting
      }
    } 这个例子 Instrument.tune(flute); //这里是Upcasting 
    因为Instrument tune(Instrument i);只接收Instrument 类型,在
    Instrument.tune(flute);的时间因为 flute 继承 Instrument,is a Instrument,Upcastingclass Instrument {
      void tune() {}
    }
    class Wind extends Instrument {
      public static void main(String[] args) {
        Wind flute = new Wind();
        flute.tune(); 
      }
    }这个就不是了     flute.tune(); 不需要吧 flute Upcasting,因为 Wind也有tune()的方法,这个只是继承
      

  2.   

    “上溯造型”的意思是否是说,在子类继承父类后,子类可以创建父类的对象来调用父类的方法。如将上面子类中
    Wind flute = new Wind();
        flute.tune(); 
    修改成
    Instrument flute=new Instrument();
      flute.tune();
    这种也可以称为“上溯造型”?另一种情况,是否“上溯造型”只能使用有参数的父类方法中,子类的对象才能调用?
      

  3.   

    class Instrument1 
    {
      public void tune() 
      {
        System.out.println("tune");  }
      Instrument1()
      {
        System.out.println("Instrument1");
      }
    }
    public class Wind1 extends Instrument1 
    {
      Wind1()
      {
        System.out.println("Wind1");
      }
      public static void main(String[] args) 
      {
        Wind1 flute = new Wind1();
        flute.tune(); //这个时间不用上溯造型,因为Wind1继承了Instrument的方法
      }
    }d:\test>java Wind1
    Instrument1
    Wind1
    tune
      

  4.   

    class Instrument 
    {
      public void play() {}
      static void tune(Instrument i) 
      {
        // ...
        i.play();
      }
    }// Wind objects are instruments
    // because they have the same interface:
    public class Wind extends Instrument 
    {
      Wind()
      {
      }
      public static void main(String[] args) 
      {
        Wind flute = new Wind();
        Instrument.tune(flute); // Upcasting 这个时间就不同了,tune(Instrument)接收Instrument的参数,而实际类型是Wind,但Wind extends Instrument,所以Wind is a Instrument,可以Upcasting
      }
    }
      

  5.   

    在这里,我们将从一个Wind句柄转换成一个Instrument句柄的行为叫作“上溯造型”。
    这句话是否就是关键?在mai()方法中,本来flute是wind中的对象,执行的是flute.tune(),而使用Intstrunment.tune(flute)来调用的话,其中(flute)就是将flute.tune()转化成Intstrument.tune(flute)来调用?这里的关键是否就是(flute)?
      

  6.   

    上溯造型的关键是:在某个方法的参数中为了面对接口(父类)编程,将参数定义为父类的形式,但由于子类is a 父类,可以使用子类的实例作为方法的参数传入,传入后实现子类调用的父类的方法,是为:上溯造型
      

  7.   

    TO addwart(灌水专用) ,建议和多态、动态绑定这些概念一起看,
    对你理解cast大有帮助!