java中clone的使用场景

Persion.java类

你好! 这是你第一次使用 **Markdown编辑器** 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识

测试类

在这里插入图片描述

测试结果

在这里插入图片描述
这里为什么list中的每一个persion对象中年龄是20呢,简单点来看,list里面存的每一个对象都是同一个persion,及每一个对象指向的是同一个地址,所以在list中的对象全部都是最后一个persion故而全部年龄都是20,进而想到了new一个对象
在这里插入图片描述
测试结果是行不通的
在这里插入图片描述
原因在于将persion赋值给persion1 同样的道理 最后得到了每一个persion1 指向的是同一个persion的地址
故而此方法是行不通的进而想到了clone()

实体类继承Cloneable并重写clone()方法
在这里插入图片描述

在这里插入图片描述
测试结果
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原型模式的使用场景是在需要创建大量相似对象的情况下,可以通过复制一个已有对象的方法来创建新的对象,避免了重复创建相似对象的开销。 下面是一个使用原型模式的示例,假设我们有一个简单的图形类 Shape,其包含一个颜色属性和一个绘制方法,我们需要创建大量不同颜色的图形对象: ```java import java.util.HashMap; import java.util.Map; public abstract class Shape implements Cloneable { private String color; public Shape(String color) { this.color = color; } public abstract void draw(); public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } } public class Circle extends Shape { private int x; private int y; private int radius; public Circle(String color, int x, int y, int radius) { super(color); this.x = x; this.y = y; this.radius = radius; } @Override public void draw() { System.out.println("Drawing Circle - Color: " + getColor() + ", X: " + x + ", Y: " + y + ", Radius: " + radius); } } public class Rectangle extends Shape { private int x; private int y; private int width; private int height; public Rectangle(String color, int x, int y, int width, int height) { super(color); this.x = x; this.y = y; this.width = width; this.height = height; } @Override public void draw() { System.out.println("Drawing Rectangle - Color: " + getColor() + ", X: " + x + ", Y: " + y + ", Width: " + width + ", Height: " + height); } } public class ShapeCache { private static Map<String, Shape> shapeMap = new HashMap<>(); public static Shape getShape(String shapeId) { Shape cachedShape = shapeMap.get(shapeId); return (Shape) cachedShape.clone(); } public static void loadCache() { Circle circle = new Circle("red", 10, 10, 20); shapeMap.put("1", circle); Rectangle rectangle = new Rectangle("blue", 5, 5, 10, 20); shapeMap.put("2", rectangle); } } public class Main { public static void main(String[] args) { ShapeCache.loadCache(); Shape clonedShape1 = ShapeCache.getShape("1"); System.out.println("Shape : " + clonedShape1.getType()); clonedShape1.draw(); Shape clonedShape2 = ShapeCache.getShape("2"); System.out.println("Shape : " + clonedShape2.getType()); clonedShape2.draw(); } } ``` 在上面的示例,我们通过在 Shape 类实现 Cloneable 接口,并重写 clone() 方法来实现原型模式。在 ShapeCache 类,我们利用 HashMap 存储已有的图形对象,并通过 getShape() 方法来获取复制后的图形对象。最后在 Main 类,我们通过 ShapeCache 类获取复制后的图形对象,并调用 draw() 方法绘制图形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值