• java继承方法规则或规律


    方法的继承与属性的继承有很大的不同,属性任何继承方式均可,而方法则有各种限制,于是在这里做了一个简单的总结:

    1、修饰符相同的方法覆盖,即只改内部,不改外部

    2、访问权限不同的方法覆盖,子类只能相对父类越来越宽松,例如父类是public,子类就不能是缺省或protect,private

    3、返回值的类型覆盖,只允许相容的返回类型,例如不能将返回值为int型改为double型,但是复合类型则例外

    4、final的方法覆盖,只能是父类无,子类有,而不能是父类有,子类无

    5、static(静态)的方法覆盖不能有任何变动,即父类有,子类必须有,父类无,子类也必须无

    实例如下:

    父类:

    package ExtendMethod;
    
    public class CommonMethod {
        protected int x=100;
        public int getX() {//定义一个普通返回值的实例方法
            return x;
        }
        public CommonMethod getObject() {//定义一个返回复合类型的方法
            return new CommonMethod();
        }
        public final void setX(int ix) {
            x=ix;
        }
        protected  void proShowMsg() {//定义一个具有保护权限的方法
            System.out.println("this is protected ShowMsg() in Common class");
        }
        
        public void pubShowMsg() {//定义一个具有公共访问权限的方法
            System.out.println("this is public showMsg() in Common class");
        }
        static public void stShowMsg() {//定义一个静态方法
            System.out.println("this is static showMsg() in Common class");
        }
    }

    一、修饰符相同的可以任意覆盖:

    public class OverrideMember_1 extends Common{
        //覆盖父类中的同名实例方法
        public void pubShowMsg() {
            System.out.println("this is public showMsg in derive class");
        }
        //覆盖父类中的同名静态方法
        static public void stShowMsg() {
            System.out.println("this is static showMsg() in derive class");
        }
        //可以任意覆盖
        public static void main(String args[]) {
            OverrideMember_1 oa =new OverrideMember_1();
            oa.pubShowMsg();
            oa.proShowMsg();
        }
    }

    二、访问权限不同的覆盖,只能越来越宽松,private——>public的方向

    public class OverrideMember_2 extends Common{
        public void proShowMsg() {//覆盖父类中的protect方法,public > protect,正确
            System.out.println("this is public ShowMsg()");
        }
        /*protected void pubShowMsg() {//权限越来越严格,错误,protect < public
            System.out.println("this is protect ShowMsg()");
        }*/
    }

    三、返回值类型的覆盖

    public class OverrideMember_3 extends Common {
        //覆盖getX()方法,但返回的数据类型不同,原方法是int型,错误
        /*public double getX() {
            return (double)x;
        }*/
        //覆盖getObject(),返回类型相容,正确,原方法的返回类型是新common对象,复合类型
        public OverrideMember_3 getObject() {
            return new OverrideMember_3();
        }
    }

    四、final的方法覆盖,只能从无到有,不能从有到无

    public class OverrideMember_4 extends Common{
        //覆盖getX()方法,并将其指定为最终方法,正确,原方法为public int getX()
        public final int  getX() {
            return x;
        }
        //覆盖最终方法,错误,原方法为 public final void setX()
        /*public void setX(int ix) {
            x=ix;
        }*/
    }

    五、static不能有任何改变

    public class OverrideMember_5 extends Common{
        //试图覆盖实例方法,将其改为静态方法,错误,原型为 public void pubShowMsg()
        /*public static void pubShowMsg() {
            System.out.println("this is public ShowMsg()");
        }*/
        //试图覆盖静态方法,并将其指定为实例方法,错误
        /*public void stShowMsg() {
            System.out.println("this is static ShowMsg()");
        }*/
    }
    //总结,静态与实例之间不能相互转换
  • 相关阅读:
    MOSS中的User的Title, LoginName, DisplayName, SID之间的关系
    如何在Network Monitor中高亮间隔时间过长的帧?
    SharePoint服务器如果需要安装杀毒软件, 需要注意什么?
    如何查看SQL Profiler? 如何查看SQL死锁?
    什么是Telnet
    The name or security ID (SID) of the domain specified is inconsistent with the trust information for that domain.
    Windows SharePoint Service 3.0的某个Web Application无搜索结果
    网络连接不上, 有TCP错误, 如果操作系统是Windows Server 2003, 请尝试一下这里
    在WinDBG中查看内存的命令
    The virtual machine could not be started because the hypervisor is not running
  • 原文地址:https://www.cnblogs.com/gambler/p/8549810.html
Copyright © 2020-2023  润新知