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

机器封所有端口 不支持做网站服务器怎么建网站

机器封所有端口 不支持做网站,服务器怎么建网站,网页制作作业成品,销客多目录 1. 深拷贝#xff08;Deep Copy#xff09; 2. 浅拷贝#xff08;Shallow Copy#xff09; 3. 深拷贝和浅拷贝的区别 4. 示例代码 浅拷贝示例 深拷贝示例 5.常用的方法 1.Java Object.clone() 方法 2.序列化与反序列化 6.Spring Boot 中的常用方法 使用 Se…目录 1. 深拷贝Deep Copy 2. 浅拷贝Shallow Copy 3. 深拷贝和浅拷贝的区别 4. 示例代码 浅拷贝示例 深拷贝示例 5.常用的方法 1.Java Object.clone() 方法 2.序列化与反序列化 6.Spring Boot 中的常用方法 使用 SerializationUtils 使用 ModelMapper 或 MapStruct 7.Hutool 工具类 浅拷贝 深拷贝 8.Guava 中的相关工具类 总结 深拷贝和浅拷贝是对象复制的两种方式它们在处理引用类型的字段时存在显著差异。下面将详细介绍它们的含义、区别并提供代码示例。 1. 深拷贝Deep Copy 定义深拷贝创建一个新对象并且复制原对象中的所有字段包括引用类型的字段。对于每个引用类型的字段它都会创建一个新的实例确保源对象和目标对象之间没有任何共享的引用。 递归地复制所有子对象。即使原始对象或拷贝对象修改其子对象也不会互相影响。 特点 基本数据类型的值会被复制。 引用数据类型的对象会被完全独立地复制。 2. 浅拷贝Shallow Copy 定义浅拷贝创建一个新对象该对象与原对象具有相同的值但对引用类型字段只会复制引用地址而不复制实际的对象。这意味着原对象和新对象的引用类型属性指向同一块内存。 不递归地复制对象所引用的子对象。它仅复制对象的基本数据类型和对引用类型的引用因此原始对象和拷贝对象中对引用类型的修改会相互影响。 特点 基本数据类型的值会被复制。 引用数据类型的对象会共享同一个实例。 3. 深拷贝和浅拷贝的区别 特性浅拷贝深拷贝对基本类型复制其值复制其值对引用类型复制引用共同使用同一对象复制对象各自独立的对象影响修改一个对象的引用类型字段会影响另一个对象修改一个对象不会影响另一个对象 4. 示例代码 浅拷贝示例 class Address {String city; ​public Address(String city) {this.city city;} } ​ class Person implements Cloneable {String name;Address address; ​public Person(String name, Address address) {this.name name;this.address address;} ​Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone(); // 浅拷贝} } ​ public class ShallowCopyExample {public static void main(String[] args) throws CloneNotSupportedException {Address address new Address(New York);Person person1 new Person(Alice, address);Person person2 (Person) person1.clone(); // 浅拷贝 ​System.out.println(Before modification:);System.out.println(Person1 Address: person1.address.city); // 输出: New YorkSystem.out.println(Person2 Address: person2.address.city); // 输出: New York ​// 修改 person2 的地址person2.address.city Los Angeles; ​System.out.println(After modification:);System.out.println(Person1 Address: person1.address.city); // 输出: Los Angeles (共享同一引用)System.out.println(Person2 Address: person2.address.city); // 输出: Los Angeles} } 深拷贝示例 class Address {String city; ​public Address(String city) {this.city city;} ​// 深拷贝方法public Address deepCopy() {return new Address(this.city);} } ​ class Person {String name;Address address; ​public Person(String name, Address address) {this.name name;this.address address;} ​// 深拷贝方法public Person deepCopy() {return new Person(this.name, this.address.deepCopy());} } ​ public class DeepCopyExample {public static void main(String[] args) {Address address new Address(New York);Person person1 new Person(Alice, address);Person person2 person1.deepCopy(); // 深拷贝 ​System.out.println(Before modification:);System.out.println(Person1 Address: person1.address.city); // 输出: New YorkSystem.out.println(Person2 Address: person2.address.city); // 输出: New York ​// 修改 person2 的地址person2.address.city Los Angeles; ​System.out.println(After modification:);System.out.println(Person1 Address: person1.address.city); // 输出: New York (不受影响)System.out.println(Person2 Address: person2.address.city); // 输出: Los Angeles} } 5.常用的方法 1.Java Object.clone() 方法 Object 类提供的 clone() 方法可以用于实现浅拷贝。 需要实现 Cloneable 接口并重写 clone() 方法。 2.序列化与反序列化 对象可以通过序列化为字节流再反序列化为新对象。这种方式可以实现深拷贝。 需要实现 Serializable 接口。 6.Spring Boot 中的常用方法 在 Spring Boot 中通常使用以下方式来实现深拷贝 使用 SerializationUtils import org.springframework.util.SerializationUtils; ​ byte[] bytes SerializationUtils.serialize(originalObject); MyObject copiedObject (MyObject) SerializationUtils.deserialize(bytes); ​ 使用 ModelMapper 或 MapStruct ModelMapper 和 MapStruct 都可以用来轻松地进行对象之间的映射借此实现深拷贝。 7.Hutool 工具类 Hutool 提供了简单易用的工具类可以进行深拷贝和浅拷贝 浅拷贝 Person copy ShallowUtil.clone(original); 深拷贝 Person deepCopy CloneUtil.clone(original); 8.Guava 中的相关工具类 Guava 提供了 Immutable 集合来避免可变性虽然不是直接的拷贝方法但可以帮助管理不可变对象。 ImmutableList、ImmutableSet和ImmutableMap 用于创建不可变的集合这样可以避免浅拷贝的问题。 不可变集合在创建后无法更改这使得它们特别适合于多线程环境和函数式编程风格。 import com.google.common.collect.ImmutableList; ​ public class ImmutableListExample {public static void main(String[] args) {// 创建一个不可变列表ImmutableListString immutableList ImmutableList.of(Apple, Banana, Cherry); ​System.out.println(immutableList); ​// 下面的操作会抛出 UnsupportedOperationException因为该列表是不可变的// immutableList.add(Date); // Uncommenting this line will throw an exception} } import com.google.common.collect.ImmutableSet; ​ public class ImmutableSetExample {public static void main(String[] args) {// 创建一个不可变集合ImmutableSetString immutableSet ImmutableSet.of(Red, Green, Blue); ​System.out.println(immutableSet); ​// 下面的操作会抛出 UnsupportedOperationException因为该集合是不可变的// immutableSet.add(Yellow); // Uncommenting this line will throw an exception} } ​ ​ import com.google.common.collect.ImmutableMap; ​ public class ImmutableMapExample {public static void main(String[] args) {// 创建一个不可变映射ImmutableMapString, Integer immutableMap ImmutableMap.of(Alice, 30,Bob, 25,Charlie, 35); ​System.out.println(immutableMap); ​// 下面的操作会抛出 UnsupportedOperationException因为该映射是不可变的// immutableMap.put(Dave, 40); // Uncommenting this line will throw an exception} } ​ 总结 浅拷贝和深拷贝分别适用于不同的场景。 根据需要选择合适的拷贝方法并利用现有的框架和库简化工作。
http://www.hkea.cn/news/14302880/

相关文章:

  • 网站常用的推广方法有哪些济南房产网官网
  • 如何查看网站是哪家公司做的?做瞹瞹嗳网站
  • 如何将网站提交给百度258网站建设
  • 网站新闻编辑怎么做国外免费wordpress主题
  • 网站添加视频外贸网站建设石家庄
  • 网站首页一般做多大尺寸广东网站建设发信息
  • 邯郸学校网站建设费用上海建站网络科技有限公司
  • 网站推广服务网址外包程序员的出路
  • 宝洁公司网站做的怎么样wordpress 调查系统
  • 免费的网站平台有哪些烘焙培训
  • jsp网站建设期末作业北京电商平台网站建设
  • 做投资理财网站wordpress注册表单
  • 网站建设公司那家好外贸流程全步骤外贸篇
  • 百度免费建网站徐州网站建设哪家好
  • 泰安网站制作电话有赞分销市场登录入口
  • 四川建设设计公司网站网站建设哪一家好
  • 做网站的 简历宁波建设行业招聘信息网站
  • 购物网站项目建设背景介绍医疗培训网站建设
  • 优化网站做什么的深圳住房与建设部网站
  • 建设网站审批做餐饮系统网站建设
  • 怎样注册网站中文域名宝安营销型网站建设公司
  • 手机网站免费制作上海排名seo公司
  • 上门做睫毛哪个网站仓山网站建设
  • wordpress 增大内存天津关键词优化效果
  • 网站建设和优化内容最重要网站发布与推广计划
  • 上海未来网站建设公司机械类网站用什么做背景
  • 如何做自己官方网站wordpress 2m
  • 有什么国外的设计网站推荐安徽住房建设厅官网信息查询
  • 网站频繁被攻击怎么办网站安全检测漏洞扫描风险等级
  • 快速开发平台开发国家优化防控措施