zoukankan      html  css  js  c++  java
  • 函数重载和多态性

    重载 overloading
    java的重载是指在一个类中同一个函数以多种不同的形态出现,即函数的参数个数或者类型不一样。
    例子 System.out.println(); 下面简称SOP
    SOP(String str)
    SOP(int number)
    SOP(char ch)
    SOP(double num)
    .................................
    这就是重载的效果

    覆盖(重写) overwritting
    覆盖即子类继承父类,然后子类中方法覆盖原父类的同名方法,从而实例化子类后,调用的是子类的方法。注意父类和子类同名方法的参数个数和类型必须完全相同.
    例子
    public class father{
    public overwritting(){
        SOP("father method");
    }

    public class son extends father{
    public overwritting(){
        SOP("son method");
    }
    public static void main(String args[]){
        father son=new son();
        son.overwritting();
    }
    }
    结果会调用子类的overwritting方法, son method。这就是覆盖。

    隐藏 hide
    那么什么情况下,子类实例化后会调用父类的方法而不是子类的方法呢?
    (当然子类和父类的方法定义需要是一样的)
    注意static 类型的方法是不能被覆盖的,所以java利用这一个特性完成了隐藏的效果。
    例子
    public class father{
    public static overwritting(){
        SOP("father method");
    }

    public class son extends father{
    public static overwritting(){
        SOP("son method");
    }
    public static void main(String args[]){
        father son=new son();
        son.overwritting();
    }
    }
    结果会调用父类的overwritting方法 fathetr method。这就是隐藏。

  • 相关阅读:
    python的logging模块
    python的random模块
    python3中time模块的用法及说明
    浅谈python模块的导入操作
    python3中,os.path模块下常用的用法总结
    OS模块的常用内置方法
    认识python中的set集合及其用法
    初识Python装饰器
    CentOS的软件包的管理之rpm和yum
    python中的函数对象与闭包函数
  • 原文地址:https://www.cnblogs.com/zcy_soft/p/1841169.html
Copyright © 2011-2022 走看看