学学习网 手机版

学学习网

学习路径: 学习首页 > 应用开发 > java >

第十二节 多态(4)

设置字体:
----------------------------------

                  {
                         this.age = age;
                  }
                  //动物会叫
                  public void cry()
                  {
                      System.out.println("不知道怎么叫!");
                   }
                  //
                  public void eat()
                  {
                       System.out.println("不知道吃什么!");
                   }
               }
               //定义猫类
               class Cat1 extends Animal1
               {
                    public void cry()
                    {
                           System.out.println("猫在叫!");
                     }
                   //猫吃东西
                    public void eat()
                    {
                          System.out.println("猫喜欢吃鱼!");
                     }
                 }
               //定义一个狗类
        class Dog1 extends Animal1
        {
                   public void cry()
                   {
                        System.out.println("汪汪叫!");
                   }
                   //狗吃东西
                   public void eat()
                  {
                        System.out.println("狗爱吃骨头");
                   }
               }
* 多态-注意事项
在讲解多态的时候,我们注意到这样一个现象-类型的转换
① java 允许父类的引用变量引用它的子类的实例(对象)
----------------------------------
课程列表
重点难点
赞助链接