zoukankan      html  css  js  c++  java
  • JAVA继承时this和super关键字

    JAVA继承时thissuper关键字

    本文主要讨论在方法前使用thissuper关键字时,编译器在什么地方查找对应的函数。

    1. 在子类中指定this关键字。首先在本类中查找,如果本类中找不到,再在父类中查找。
    class A
    
    {
    
        public void fun()
    
        {
    
            System.out.println("父类的fun()");
    
        }
    
    }
    
    class B extends A
    
    {
    
        public void test()
    
        {
    
            this.fun();
    
        }
    
        public void fun()
    
        {
    
            System.out.println("子类中的fun()");
    
        }
    
    }
    
    public class Hello
    
    {
    
        public static void main(String[] args) throws Exception
    
        {
    
            B b = new B();
    
            b.test();
    
        }
    
    }

    输出结果是子类中的fun。如果我们将子类中的该函数删去,那么调用的就是父类中的fun

    class A
    
    {
    
        public void fun()
    
        {
    
            System.out.println("父类的fun()");
    
        }
    
    }
    
    class B extends A
    
    {
    
        public void test()
    
        {
    
            this.fun();
    
        }
    
        // public void fun()
    
        // {
    
        // System.out.println("子类中的fun()");
    
        // }
    
    }
    
    public class Hello
    
    {
    
        public static void main(String[] args) throws Exception
    
        {
    
            B b = new B();
    
            b.test();
    
        }
    
    }

    输出父类中的fun

    1. 在子类中使用super,那么跳过子类查找直接从父类中查找
    class A
    
    {
    
        public void fun()
    
        {
    
            System.out.println("父类的fun()");
    
        }
    
    }
    
    class B extends A
    
    {
    
        public void test()
    
        {
    
            super.fun();
    
        }
    
        public void fun()
    
        {
    
            System.out.println("子类中的fun()");
    
        }
    
    }
    
    public class Hello
    
    {
    
        public static void main(String[] args) throws Exception
    
        {
    
            B b = new B();
    
            b.test();
    
        }
    
    }

    输出父类中的fun

    1. 在父类中指定this关键字。和子类中指定this关键字一样,先从子类中找覆写的方法,如果找到,直接调用子类的该方法,如果没找到再从父类中寻找。
    class A
    
    {
    
        public void test()
    
        {
    
            this.fun();
    
        }
    
        public void fun()
    
        {
    
            System.out.println("父类的fun()");
    
        }
    
    }
    
    class B extends A
    
    {
    
        public void fun()
    
        {
    
            System.out.println("子类中的fun()");
    
        }
    
    }
    
    public class Hello
    
    {
    
        public static void main(String[] args) throws Exception
    
        {
    
            B b = new B();
    
            b.test();
    
        }
    
    }

    输出子类中的fun

    如果此时将子类中的fun删除

    class A
    
    {
    
        public void test()
    
        {
    
            this.fun();
    
        }
    
        public void fun()
    
        {
    
            System.out.println("父类的fun()");
    
        }
    
    }
    
    class B extends A
    
    {
    
        // public void fun()
    
        // {
    
        // System.out.println("子类中的fun()");
    
        // }
    
    }
    
    public class Hello
    
    {
    
        public static void main(String[] args) throws Exception
    
        {
    
            B b = new B();
    
            b.test();
    
        }
    
    }

    输出父类中的fun

    但此时有一点需要注意的是,如果修改父类的fun的访问权限为private,这时输出的就是父类中的fun

    class A
    
    {
    
        public void test()
    
        {
    
            this.fun();
    
        }
    
        private void fun()
    
        {
    
            System.out.println("父类的fun()");
    
        }
    
    }
    
    class B extends A
    
    {
    
        public void fun()
    
        {
    
            System.out.println("子类中的fun()");
    
        }
    
    }
    
    public class Hello
    
    {
    
        public static void main(String[] args) throws Exception
    
        {
    
            B b = new B();
    
            b.test();
    
        }
    
    }
  • 相关阅读:
    AIX上解压缩.tar.Z, .tar.gz, .zip及.tgz
    linux用rdate命令实现同步时间
    ssh-copy-id:/usr/bin/ssh-copy-id: ERROR: No identities found
    开源权限管理-源码介绍
    C#设计模式学习资料--装饰模式
    C#设计模式学习资料--外观模式
    C#设计模式学习资料--原型模式
    C#设计模式学习资料--创建者模式
    C#设计模式学习资料--观察者模式
    C#设计模式学习资料--模版方式模式
  • 原文地址:https://www.cnblogs.com/kuillldan/p/5905618.html
Copyright © 2011-2022 走看看