zoukankan      html  css  js  c++  java
  • 第10章 多态

    第10章 多态

    本章重点:
    1、多态
    2、动态绑定和静态绑定
    3、超类

    多态就是拥有多种形态。
    在Java语言中,多态主要 是指拥有相同的形式,但不同的参数实现不同的功能。

    多态的规则:
    1、方法名称一定要一样。
    2、传入的参数类型一定要不一样。

    public class student{
    int x;
    int y;
    int z;

    void print(int x){
    System.out.println(2 * x);
    }
    void print(int x, int y){
    System.out.println(2 * x + y);
    }
    public static void main(String[] args){
    student st = new student();
    st.print(2);
    st.print(2, 3);
    }
    }

    继承与多态:

    public class studentA{
    void print(){
    System.out.println("这是一个方法");
    }
    public static void main(String[] args){
    studentA sta = new studentA();
    studentB stb = new studentB();
    sta.print();
    stb.print();
    }
    }

    class studentB extends studentAP{
    void print(){
    System.out.println("这是一个子类");
    }
    }

    所谓绑定就是对象句柄与方法的绑定。
    绑定分为静态绑定和动态绑定。
    静态绑定不存在多态的问题。
    动态绑定存在多态的问题。

    equals方法的使用:
    1、自反性。
    2、对称性。
    3、传递性。
    4、一致性。

    重载:一个类里,名字相同但参数不同的方法。
    多态:避免在父类里大量重载,而引起代码臃肿且难于维护的解决方案。
    多态的两种表现形式:重载和覆盖。

  • 相关阅读:
    C# 协变 逆变
    go slice 理解
    为什么避免使用Task.Wait或者Task.Result
    IL笔记
    docker随笔
    领域事件
    总结笔记
    基于ASP.NET Core 3.x的端点路由(Endpoint Routing)实现控制器(Controller)和操作(Action)分离的接口服务
    CentOS7 多IP搭建SOCKS5代理服务器
    Springboot 实现数据库备份还原
  • 原文地址:https://www.cnblogs.com/QQ9888267/p/6115250.html
Copyright © 2011-2022 走看看