• java 子类重写函数的几个测试


    public class Test {
           public static void main (String[] args) {
               Animal h = new Horse();
               h.eat();  
           }
       }
    
       class Animal {
           public void eat(){
               System.out.println ("Animal is eating.");
           }
       }
      
       class Horse extends Animal{
           public void eat(){
               System.out.println ("Horse is eating.");
           }
           public void buck(){
           }
       }
    

      一个原则是:使用了什么引用,编译器就会只调用引用类所拥有的方法。如果调用子类特有的方法,如上例的h.buck(); 编译器会抱怨的(编译错误)。也就是说,编译器只看引用类型,而不是对象类型。

    下面是有关继承中重写的几个测试

    1.函数同等权限,父类引用,子类实例;

    public class HelloWorld{
    
         public static void main(String []args){
            Animal A = new Horse();
            A.eat();
         }
    }
    class Animal
    {
        public void eat()
        {
            System.out.println("A is eating");
        }
    }
    class Horse extends Animal
    {
        public void eat()
        {
            System.out.println("B is eating");
        }
    }
    

      编译执行

    sh-4.3$ javac HelloWorld.java                                                                                                            
    sh-4.3$ javac HelloWorld.java                                                                                                            
    sh-4.3$ java -Xmx128M -Xms16M HelloWorld                                                                                                 
    B is eating 
    没有问题
    2.函数同等权限,子类引用,父类实例
    public class HelloWorld{
    
         public static void main(String []args){
            //Animal A = new Horse();
            Horse A = new Animal();
            A.eat();
         }
    }
    class Animal
    {
        public void eat()
        {
            System.out.println("A is eating");
        }
    }
    class Horse extends Animal
    {
        public void eat()
        {
            System.out.println("B is eating");
        }
    }
    

      编译报错:

    sh-4.3$ javac HelloWorld.java                                                                                                            
    HelloWorld.java:5: error: incompatible types: Animal cannot be converted to Horse                                                        
            Horse A = new Animal();                                                                                                          
                      ^                                                                                                                      
    1 error  
    向下转型不安全,子类引用不能指向父类对象;
    3.函数不同权限,父类引用,子类实例
    public class HelloWorld{
    
         public static void main(String []args){
            Animal A = new Horse();
            //Horse A = new Animal();
            A.eat();
         }
    }
    class Animal
    {
        protected void eat()
        {
            System.out.println("A is eating");
        }
    }
    class Horse extends Animal
    {
        public void eat()
        {
            System.out.println("B is eating");
        }
    }
    

      

                                                                                       

    编译执行
    sh-4.3$ javac HelloWorld.java                                                                                                            
    sh-4.3$ java -Xmx128M -Xms16M HelloWorld                                                                                                 
    B is eating
    没有问题,重写的权限要比父类函数的权限相等或更宽。
  • 相关阅读:
    2018 校招在线编程 20题-01
    ubuntu 配置muduo库
    plsql远程访问配置
    web开发转发和重定向大比拼
    Eclipse中svn同步忽略设置
    静态方法、实例方法和域
    接口中的域
    屏蔽所有异常的方法
    使用axis2时在temp文件产生大量缓存
    spring boot redis分布式锁 (转)
  • 原文地址:https://www.cnblogs.com/TaoChiangBlog/p/6398052.html
Copyright © 2020-2023  润新知