首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,085 阅读
2
类的加载
741 阅读
3
Spring Cloud OAuth2.0
726 阅读
4
SpringBoot自动装配原理
691 阅读
5
集合不安全问题
586 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
389
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
页面
统计
关于
搜索到
1
篇与
的结果
2022-08-22
里氏替换原则
定义:子类可以扩展父类的功能,但不能改变父类原有的功能(子类继承父类时,除了添加新的方法和功能外,尽量不要重写父类的方法)以正方形不是长方形为例:在resize方法中,Rectangle类型的参数不能被Square类型的参数所代替,如果进行了替换,则不能得到预期的打印结果Rectanglepublic class Rectangle { private double length; private double width; public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } }Squarepublic class Square extends Rectangle { @Override public void setLength(double length) { super.setLength(length); super.setWidth(length); } @Override public void setWidth(double width) { super.setWidth(width); super.setLength(width); } }RectangleDemopublic class RectangleDemo { public static void main(String[] args) { //创建长方形对象 Rectangle rectangle = new Rectangle(); rectangle.setLength(20); rectangle.setWidth(10); //扩宽 resize(rectangle); printLengthAndWidth(rectangle); System.out.println("===================="); //创建正方形 Square square = new Square(); square.setWidth(20); resize(square); //在resize方法中,Rectangle类型的参数不能被Square类型的参数所代替,如果进行了替换,则不能得到预期的打印结果 printLengthAndWidth(square); } public static void resize(Rectangle rectangle) { //如果长 > 宽,进行扩宽操作 while (rectangle.getLength() >= rectangle.getWidth()) { rectangle.setWidth(rectangle.getWidth() + 1); } } public static void printLengthAndWidth(Rectangle rectangle) { System.out.println("长:" + rectangle.getLength()); System.out.println("宽:" + rectangle.getWidth()); } }改进:抽象出四边形接口,长方形、正方形实现四边形接口Quadrilateralpublic interface Quadrilateral { /** * 获取长 * * @return */ double getLength(); /** * 获取宽 * * @return */ double getWidth(); }Rectanglepublic class Rectangle implements Quadrilateral { private double length; private double width; public void setLength(double length) { this.length = length; } public void setWidth(double width) { this.width = width; } @Override public double getLength() { return length; } @Override public double getWidth() { return width; } }Squarepublic class Square implements Quadrilateral { private double side; public double getSide() { return side; } public void setSide(double side) { this.side = side; } @Override public double getLength() { return side; } @Override public double getWidth() { return side; } }RectangleDemopublic class RectangleDemo { public static void main(String[] args) { //创建长方形对象 Rectangle rectangle = new Rectangle(); rectangle.setLength(20); rectangle.setWidth(10); resize(rectangle); printLengthAndWidth(rectangle); System.out.println("===================="); //创建正方形对象(此时正方形和长方形不存在父子关系) Square square = new Square(); square.setSide(10); printLengthAndWidth(square); } public static void resize(Rectangle rectangle) { //如果长 > 宽,进行扩宽操作 while (rectangle.getLength() >= rectangle.getWidth()) { rectangle.setWidth(rectangle.getWidth() + 1); } } public static void printLengthAndWidth(Quadrilateral quadrilateral) { System.out.println("长:" + quadrilateral.getLength()); System.out.println("宽:" + quadrilateral.getWidth()); } }
2022年08月22日
22 阅读
0 评论
0 点赞