当前位置: 首页 > news >正文

多态语法详解

多态语法详解

  • 一:概念
    • 1:多态实现条件
  • 二:重写:
  • 三:向上转型和向下转型
    • 1:向上转型:
      • 1:直接赋值:
      • 2:方法传参
      • 3:返回值
    • 2:向下转型

一:概念

1:同一个引用,调用了同一个方法,因为引用的对象不一样,所表现出来的行为也不一样。

1:多态实现条件

1:必须在继承体系下;
2:子类必须对父类中的方法进行重写;
3:通过父类引用调用重写的方法;

二:重写:

重写也称覆盖。重写是子类对父类非静态,非private,非final修饰,非构造方法等的实现过程进行重新编写。
重写规则
1:方法名,参数列表(参数类型,个数,顺序),返回类型都要相同,(返回类型可以构成父子类关系)。
2:子类重写父类同名的方法时,子类方法的访问权限要大于父类的。
3:当在父类的构造方法中,调用了子类和父类同名的方法时,此时会调用子类的方法。
提醒: 不要在构造方法中调用重写的方法。

class Person{public String name;public int age;public Person(String name, int age) {this.name = name;this.age = age;fun();}public void fun(){System.out.println("父类的fun()方法");}
}
class Student extends Person{public Student(String name, int age) {super(name, age);}public void fun(){System.out.println("子类的fun()方法");}
}
public class Test {public static void main(String[] args) {Student student=new Student("张三",20);}
}

在这里插入图片描述4:父类方法被static ,final,private修饰不能重写

三:向上转型和向下转型

1:向上转型:

子类对象给到了父类对象,也可以理解为:父类引用引用的是子类对象,通过父类的引用去调用父类和子类同名的方法,不过调用的是子类的方法。(也叫作动态绑定)

1:直接赋值:

class Animal{private String name;private int age;public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
public class Test {public static void main(String[] args) {Animal animal=new Dog("旺财",3);//父类引用引用了子类对象animal.eat();//通过父类引用访问了和父类同名的子类方法,}
}

在这里插入图片描述

2:方法传参

class Animal{private String name;private int age;public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}
}
public class Test {public static void fun(Animal animal){animal.eat();//同一个引用,引用了同一个方法,因为引用的对象不一样,所表现出来的行为不一样,我们把这种思想叫做多态}public static void main(String[] args) {Dog dog=new Dog("旺财",3);fun(dog);fun(new Cat("喵喵",2));}
}

在这里插入图片描述

3:返回值

作返回值,返回任意子类对象

class Animal{private String name;private int age;public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}
}
public class Test {public static Animal fun(){return new Dog("旺财",3);}public static void main(String[] args) {Animal animal=fun();animal.eat();}
}

2:向下转型

将一个子类对象经过向上转型后当成父类方法使用,再也无法调用子类特有的方法,

class Animal{private String name;private int age;public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}public void barks(){System.out.println(this.getName()+"摇尾巴");}
}
public class Test {public static void main(String[] args) {Animal animal =new Dog("旺财",3);animal.barks();}
}

在这里插入图片描述

但有时需要调用子类特有的方法,此时:将父类引用在还原为子类对象,也就是向下转型。

class Animal{private String name;private int age;public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}public void barks(){System.out.println(this.getName()+"摇尾巴");}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}}
public class Test {public static void main(String[] args) {Dog dog=new Dog("旺财" ,2);Animal animal =dog;dog=**(Dog)** animal;dog.barks();}
}

在这里插入图片描述向下转型用的比较少,而且不完全,万一转换失败,运行时就会抛出异常,Java中为了提高向下转型的安全性,引入了instance,如果表达式为true,则可以安全转换。

class Animal{private String name;private int age;public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(){System.out.println(this.age+"在吃饭");}
}
class Dog extends Animal{public Dog(String name, int age) {super(name, age);}public void barks(){System.out.println(this.getName()+"摇尾巴");}@Overridepublic void eat() {System.out.println(this.getName()+"吃狗粮");}
}
class Cat extends Animal{public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.getName()+"吃猫粮");}}
public class Test {public static void main(String[] args) {Animal animal = new Dog("旺财", 3);if (animal instanceof Dog) {Dog dog = (Dog) animal;((Dog) animal).barks();}}
}

在这里插入图片描述

相关文章:

多态语法详解

多态语法详解 一:概念1:多态实现条件 二:重写:三:向上转型和向下转型1:向上转型:1:直接赋值:2:方法传参3:返回值 2:向下转型 一:概念 1:同一个引…...

Python大数据之linux学习总结——day11_ZooKeeper

ZooKeeper ZK概述 ZooKeeper概念: Zookeeper是一个分布式协调服务的开源框架。本质上是一个分布式的小文件存储系统 ZooKeeper作用: 主要用来解决分布式集群中应用系统的一致性问题。 ZooKeeper结构: 采用树形层次结构,ZooKeeper树中的每个节点被称为—Znode。且树…...

C语言——函数的嵌套调用

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>void new_line() {printf("Hello\n"); }void three_line() {int i0;for(i0;i<3;i){new_line();} }int main() {three_line();return 0; }...

4种经典的限流算法与集群限流

0、基础知识 1000毫秒内&#xff0c;允许2个请求&#xff0c;其他请求全部拒绝。 不拒绝就可能往db打请求&#xff0c;把db干爆~ interval 1000 rate 2&#xff1b; 一、固定窗口限流 固定窗口限流算法&#xff08;Fixed Window Rate Limiting Algorithm&#xff09;是…...

网工内推 | 国企、港企网工,年底双薪,NA以上认证即可

01 中航期货有限公司 招聘岗位&#xff1a;信息技术部-网络工程师 职责描述&#xff1a; 1、负责总部、分支机构、外联单位网络的日常运维、故障和应急处置&#xff0c;特别是定期监测设备的运行状态&#xff0c;对存在隐患的地方及时发现改正&#xff0c;保持网络稳定通畅&am…...

【华为HCIP | 华为数通工程师】刷题日记1116(一个字惨)

个人名片&#xff1a; &#x1f43c;作者简介&#xff1a;一名大三在校生&#xff0c;喜欢AI编程&#x1f38b; &#x1f43b;‍❄️个人主页&#x1f947;&#xff1a;落798. &#x1f43c;个人WeChat&#xff1a;hmmwx53 &#x1f54a;️系列专栏&#xff1a;&#x1f5bc;️…...

​软考-高级-系统架构设计师教程(清华第2版)【第7章 系统架构设计基础知识(263~285)-思维导图】​

软考-高级-系统架构设计师教程&#xff08;清华第2版&#xff09;【第7章 系统架构设计基础知识&#xff08;263~285&#xff09;-思维导图】 课本里章节里所有蓝色字体的思维导图...

⑩⑥ 【MySQL】详解 触发器TRIGGER,协助 确保数据的完整性,日志记录,数据校验等操作。

个人简介&#xff1a;Java领域新星创作者&#xff1b;阿里云技术博主、星级博主、专家博主&#xff1b;正在Java学习的路上摸爬滚打&#xff0c;记录学习的过程~ 个人主页&#xff1a;.29.的博客 学习社区&#xff1a;进去逛一逛~ 触发器 ⑩⑥ 【MySQL】触发器详解1. 什么是触发…...

数据结构与算法编程题3

长度为n的顺序表&#xff0c;删除线性表所有值为x的元素&#xff0c;使得时间复杂度为O(n)&#xff0c;空间复杂度为O(1) #include <iostream> using namespace std;typedef int ElemType; #define Maxsize 100 #define OK 1 #define ERROR 0 typedef struct SqList {E…...

Go基础面经大全(持续补充中)

Go基础 1. 基础特性 Go的优势 天生支持并发&#xff0c;性能高。 单一的标准代码格式&#xff0c;比其他语言更具可读性。 自动垃圾收集机制比Java和Python更有效&#xff0c;因为它与程序同时执行。 Go数据类型 int, string, float, bool, array, slice, map, channel, p…...

uniapp heckbox-group实现多选

文章目录 html 代码JS 代码 混了业务逻辑&#xff0c;谨慎观看 html 代码 <view><!--可滚动视图区域。用于区域滚动 --><scroll-view :style"{ height: clientHeight px }" :scroll-top"scrollTop" scroll-y"true"scrolltouppe…...

读懂:“消费报销”模式新零售打法,适用连锁门店加盟的营销方案

读懂&#xff1a;“消费报销”模式新零售打法&#xff0c;适用连锁门店加盟的营销方案 引言&#xff1a;2023年的双十一已经落下帷幕&#xff0c;作为每年的经典电商促销节&#xff0c;今年已是第15个年头&#xff0c;但是今年各大电商平台却都是非常默契的&#xff0c;没有公布…...

一个基本的http客户端

高可用 客户端 1. httpClient.h #include <iostream> #include <string> #include <functional>class HttpClient { public:HttpClient(std::string url) : url_(url), port_(0) {}int write_http(const std::string &method, const std::string &…...

html-网站菜单-点击菜单展开相应的导航栏,加减号可切换

一、效果图 1.点击显示菜单栏&#xff0c;点击x号关闭&#xff1b; 2.点击一级菜单&#xff0c;展开显示二级&#xff0c;并且加号变为减号&#xff1b; 3.点击其他一级导航&#xff0c;自动收起展开的导航。 二、代码实现 <!DOCTYPE html> <html><head>&…...

2.FastRunner定时任务Celery+RabbitMQ

注意&#xff1a;celery版本和Python冲突问题 不能用高版本Python 用3.5以下&#xff0c;因为项目的celery用的django-celery 3.2.2 python3.7 async关键字 冲突版本 celery3.x方案一&#xff1a; celery3.xpython3.6方案二 &#xff1a; celery4.xpython3.7 解决celery执…...

vb.net 实时监控双门双向门禁控制板源代码

本示例使用设备介绍&#xff1a;实时网络双门双向门禁控制板可二次编程控制网络继电器远程开关-淘宝网 (taobao.com) Imports System.Net.Sockets Imports System.Net Imports System.Text Imports System.ThreadingImports System.Net.NetworkInformation Imports System.Man…...

文具办公产品展示预约小程序的作用如何

从整体来看&#xff0c;文具办公品牌/门店的生意来源于线下自然流量或线上自营商城/入驻第三方商城的的流量&#xff0c;线上多数情况都是以直接销售配送为主&#xff0c;但其实对文具品牌/门店而言还有信息展示、服务预约、在线咨询、产品介绍等需求。 虽然小区周边的消费者需…...

渗透测试流程是什么?7个步骤给你讲清楚!

在学习渗透测试之初&#xff0c;有必要先系统了解一下它的流程&#xff0c;静下心来阅读一下&#xff0c;树立一个全局观&#xff0c;一步一步去建设并完善自己的专业领域&#xff0c;最终实现从懵逼到牛逼的华丽转变。渗透测试是通过模拟恶意黑客的攻击方法&#xff0c;同时也…...

如何解决网站被攻击的问题:企业网络攻防的关键路径

在当今数字化时代&#xff0c;企业面临着不断升级的网络威胁&#xff0c;网站遭受攻击的风险也与日俱增。解决网站被攻击的问题对企业发展至关重要&#xff0c;不仅关系到企业的信息安全&#xff0c;也直接影响到企业的声誉和利益。从企业发展的角度出发&#xff0c;我们将探讨…...

大健康产业的先行者「完美公司」携手企企通,推进企业采购供应链数字化进程

随着中国经济持续向好&#xff0c;消费升级和美妆步骤增加&#xff0c;美妆和个人护理产品已逐渐成为中国消费者的日用消费品&#xff0c;推动了护肤品和化妆品的销售额增速均超过10%&#xff0c;成为中国整个快速消费品市场中的一颗亮眼明珠。 据国家统计局数据显示&#xff0…...

conda相比python好处

Conda 作为 Python 的环境和包管理工具&#xff0c;相比原生 Python 生态&#xff08;如 pip 虚拟环境&#xff09;有许多独特优势&#xff0c;尤其在多项目管理、依赖处理和跨平台兼容性等方面表现更优。以下是 Conda 的核心好处&#xff1a; 一、一站式环境管理&#xff1a…...

系统设计 --- MongoDB亿级数据查询优化策略

系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log&#xff0c;共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题&#xff0c;不能使用ELK只能使用…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

Springcloud:Eureka 高可用集群搭建实战(服务注册与发现的底层原理与避坑指南)

引言&#xff1a;为什么 Eureka 依然是存量系统的核心&#xff1f; 尽管 Nacos 等新注册中心崛起&#xff0c;但金融、电力等保守行业仍有大量系统运行在 Eureka 上。理解其高可用设计与自我保护机制&#xff0c;是保障分布式系统稳定的必修课。本文将手把手带你搭建生产级 Eur…...

Map相关知识

数据结构 二叉树 二叉树&#xff0c;顾名思义&#xff0c;每个节点最多有两个“叉”&#xff0c;也就是两个子节点&#xff0c;分别是左子 节点和右子节点。不过&#xff0c;二叉树并不要求每个节点都有两个子节点&#xff0c;有的节点只 有左子节点&#xff0c;有的节点只有…...

稳定币的深度剖析与展望

一、引言 在当今数字化浪潮席卷全球的时代&#xff0c;加密货币作为一种新兴的金融现象&#xff0c;正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而&#xff0c;加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下&#xff0c;稳定…...

C# 表达式和运算符(求值顺序)

求值顺序 表达式可以由许多嵌套的子表达式构成。子表达式的求值顺序可以使表达式的最终值发生 变化。 例如&#xff0c;已知表达式3*52&#xff0c;依照子表达式的求值顺序&#xff0c;有两种可能的结果&#xff0c;如图9-3所示。 如果乘法先执行&#xff0c;结果是17。如果5…...

【Linux系统】Linux环境变量:系统配置的隐形指挥官

。# Linux系列 文章目录 前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变量的生命周期 四、环境变量的组织方式五、C语言对环境变量的操作5.1 设置环境变量&#xff1a;setenv5.2 删除环境变量:unsetenv5.3 遍历所有环境…...

掌握 HTTP 请求:理解 cURL GET 语法

cURL 是一个强大的命令行工具&#xff0c;用于发送 HTTP 请求和与 Web 服务器交互。在 Web 开发和测试中&#xff0c;cURL 经常用于发送 GET 请求来获取服务器资源。本文将详细介绍 cURL GET 请求的语法和使用方法。 一、cURL 基本概念 cURL 是 "Client URL" 的缩写…...

什么是VR全景技术

VR全景技术&#xff0c;全称为虚拟现实全景技术&#xff0c;是通过计算机图像模拟生成三维空间中的虚拟世界&#xff0c;使用户能够在该虚拟世界中进行全方位、无死角的观察和交互的技术。VR全景技术模拟人在真实空间中的视觉体验&#xff0c;结合图文、3D、音视频等多媒体元素…...