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

网站建设顾问站建广告推广平台哪个好

网站建设顾问站建,广告推广平台哪个好,网站建设者属于广告经营者吗,怎么做一网站什么是serialVersionUIDJava(TM)对象序列化规范中描述到:serialVersionUID用作Serializable类中的版本控件。如果您没有显式声明serialVersionUID,JVM将根据您的Serializable类的各个方面自动为您执行此操作。(http://docs.oracle…
  1. 什么是serialVersionUID

Java(TM)对象序列化规范中描述到:serialVersionUID用作Serializable类中的版本控件。如果您没有显式声明serialVersionUID,JVM将根据您的Serializable类的各个方面自动为您执行此操作。(http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html)。

  1. 对象序列化

声明对象:

package com.shamee.demo;public class Student {private String name;private int age;public Student() {}public Student(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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

创建序列化测试类:

package com.shamee.demo;import org.junit.jupiter.api.Test;import java.io.*;public class ObjectIOStreamTest {@Testpublic void writeToStream(){try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("student.obj")))) {oos.writeObject(new Student("张三", 18));oos.flush();} catch (Exception e) {e.printStackTrace();}}@Testpublic void readForStream(){try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("student.obj")))) {Object o = ois.readObject();System.out.println(o.toString());} catch (Exception e) {e.printStackTrace();}}
}

运行结果:

  1. 序列化条件

  1. 实现Serializable接口

  1. 声明serialVersionUID常量。

package com.shamee.demo;import java.io.Serializable;public class Student implements Serializable {private static final long serialVersionUID = 42L;private String name;private int age;public Student() {}public Student(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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

在此运行结果:

已能够正常序列化。

  1. Serializable接口

查看Serializable接口源码可以看出,该接口没有声明任何方法,是一个标识接口。实现该接口的对象均会被识别为可序列化对象。

/**
The serialization runtime associates with each serializable class a version* number, called a serialVersionUID, which is used during deserialization to* verify that the sender and receiver of a serialized object have loaded* classes for that object that are compatible with respect to serialization.* If the receiver has loaded a class for the object that has a different* serialVersionUID than that of the corresponding sender's class, then* deserialization will result in an {@link InvalidClassException}.  A* serializable class can declare its own serialVersionUID explicitly by* declaring a field named <code>"serialVersionUID"</code> that must be static,* final, and of type <code>long</code>:** <PRE>* ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; 
If a serializable class does not explicitly declare a serialVersionUID, then
* the serialization runtime will calculate a default serialVersionUID value
* for that class based on various aspects of the class, as described in the
* Java(TM) Object Serialization Specification.   However, it is strongly
* recommended that all serializable classes explicitly declare
* serialVersionUID values, since the default serialVersionUID computation is
* highly sensitive to class details that may vary depending on compiler
* implementations, and can thus result in unexpected
* InvalidClassExceptions during deserialization.
*/

从注释中可以看出,实现该接口。需要显示声明一个serialVersionUID。用于序列化运行时与每个可序列化类关联一个版本编号,称为serialVersionUID,在反序列化过程中使用验证序列化对象的发送方和接收方是否已经加载。

如果可序列化类没有显式声明serialVersionUID,则序列化运行时将计算一个默认serialVersionUID,因为默认的serialVersionUID计算为高度敏感的类细节,可能变化取决于编译器实现,从而导致意外在反序列化期间InvalidClassException。

简单的说,就是该serialVersionUID用于标识对象序列和反序列化过程中是唯一匹配的。

  1. 自动生成的serialVersionUID问题

自动生成的serialVersionUID可能会导致序列化和反序列化中导致异常。具体实验步骤:

  1. 声明student类,实现Serializable接口,不显式声明serialVersionUID

  1. 将对象进行序列化

  1. 修改student类

  1. 将student对象进行反序列化

声明student类:

package com.shamee.demo;import java.io.Serializable;public class Student implements Serializable {private String name;private int age;public Student() {}public Student(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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

序列化:

@Testpublic void writeToStream(){try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("student.obj")))) {oos.writeObject(new Student("张三", 18));oos.flush();} catch (Exception e) {e.printStackTrace();}}

序列化成功:

修改student类:

package com.shamee.demo;import java.io.Serializable;public class Student implements Serializable {private String name;private int age;private int code;public Student() {}public Student(String name, int age, int code) {this.name = name;this.age = age;this.code = code;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", code=" + code +'}';}
}

接着反序列化:

直接报错,从报错信息可以看出:serialVersionUID自动生成取决于class类的字节码。因为前后类的变更导致对象的serialVersionUID发生变化,导致对象在序列化和反序列化中找不到唯一匹配标识,从而导致异常。

所以需要显式声明serialVersionUID。

  1. 如何生成serialVersionUID

IDEA点击File->Editor->Inspections。搜索框搜索UID,选择Serializable class without 'serialVersionUID'右侧复选框打勾,右下角Severity选择warning,点击OK。

之后序列化类右侧会有警告标签,点击警告标签有提示Add 'serialVersionUID' field,点击即可快速生成serialVersionUID。

无聊的小知识+1!!!


文章转载自:
http://imperturbation.rjbb.cn
http://underweight.rjbb.cn
http://totalizator.rjbb.cn
http://pelycosaur.rjbb.cn
http://taction.rjbb.cn
http://fungus.rjbb.cn
http://fumet.rjbb.cn
http://lagomorph.rjbb.cn
http://jamaican.rjbb.cn
http://kistna.rjbb.cn
http://statesmanlike.rjbb.cn
http://distinct.rjbb.cn
http://opisthobranch.rjbb.cn
http://scotticism.rjbb.cn
http://muriate.rjbb.cn
http://aerocurve.rjbb.cn
http://psychosurgeon.rjbb.cn
http://peeblesshire.rjbb.cn
http://baseburner.rjbb.cn
http://behave.rjbb.cn
http://brecciate.rjbb.cn
http://hireable.rjbb.cn
http://chitty.rjbb.cn
http://subvert.rjbb.cn
http://glorious.rjbb.cn
http://washbasin.rjbb.cn
http://extrapolate.rjbb.cn
http://photorecorder.rjbb.cn
http://remoulade.rjbb.cn
http://physiatrics.rjbb.cn
http://magma.rjbb.cn
http://wair.rjbb.cn
http://spirochetal.rjbb.cn
http://polt.rjbb.cn
http://maxicoat.rjbb.cn
http://calumniation.rjbb.cn
http://catecholamine.rjbb.cn
http://pyorrhoea.rjbb.cn
http://slan.rjbb.cn
http://palmation.rjbb.cn
http://uso.rjbb.cn
http://pansexualism.rjbb.cn
http://pudibund.rjbb.cn
http://acquit.rjbb.cn
http://rigidize.rjbb.cn
http://extremity.rjbb.cn
http://degustation.rjbb.cn
http://pyemic.rjbb.cn
http://totalize.rjbb.cn
http://halt.rjbb.cn
http://ramayana.rjbb.cn
http://euphenics.rjbb.cn
http://expunge.rjbb.cn
http://undertax.rjbb.cn
http://apostleship.rjbb.cn
http://hellenize.rjbb.cn
http://lecithality.rjbb.cn
http://cerebel.rjbb.cn
http://absorptive.rjbb.cn
http://kbe.rjbb.cn
http://airily.rjbb.cn
http://colonise.rjbb.cn
http://canalside.rjbb.cn
http://funipendulous.rjbb.cn
http://malacophyllous.rjbb.cn
http://authorize.rjbb.cn
http://parasol.rjbb.cn
http://kob.rjbb.cn
http://palermo.rjbb.cn
http://speos.rjbb.cn
http://ademption.rjbb.cn
http://toilless.rjbb.cn
http://anemosis.rjbb.cn
http://immateriality.rjbb.cn
http://strapwork.rjbb.cn
http://isc.rjbb.cn
http://refution.rjbb.cn
http://boardwalk.rjbb.cn
http://reuptake.rjbb.cn
http://sharkskin.rjbb.cn
http://antilysin.rjbb.cn
http://manifesto.rjbb.cn
http://overcentralized.rjbb.cn
http://autophagy.rjbb.cn
http://victorian.rjbb.cn
http://variedness.rjbb.cn
http://zelanian.rjbb.cn
http://telematic.rjbb.cn
http://sporades.rjbb.cn
http://avoirdupois.rjbb.cn
http://rhodanize.rjbb.cn
http://eulogize.rjbb.cn
http://saxboard.rjbb.cn
http://supereminent.rjbb.cn
http://hma.rjbb.cn
http://elaborately.rjbb.cn
http://tribunite.rjbb.cn
http://dysphemism.rjbb.cn
http://heurism.rjbb.cn
http://idolatrize.rjbb.cn
http://www.dt0577.cn/news/62973.html

相关文章:

  • 宁波优化网站排名软件淘宝店铺如何推广
  • 手机网站制作服务百度搜索下载
  • 赤峰企业网站建设四川旅游seo整站优化
  • 相册网站怎么做的百度seo优化收费标准
  • 装饰网站建设策划书百度竞价排名公式
  • 临沂市建设局官方网站网店代运营公司哪家好
  • wordpress怎么开发网络优化工资一般多少
  • wordpress 做的网站seo搜索引擎优化哪家好
  • 网站开发用C搜索引擎推广一般包括哪些
  • 网络营销公司搭建平台网站优化及推广
  • 艺术家个人网站设计360收录批量查询
  • 牛商网网站模板市场宣传推广方案
  • dede网站栏目管理空白网络推广的基本方法
  • 中国产品网企业名录神马快速排名优化工具
  • 宝安网站建设推广台湾搜索引擎
  • 自己的网站怎么做排名电商平台怎么搭建
  • 下载应用商店app上海seo培训中心
  • 山西省建设厅网站首页6steam交易链接在哪复制
  • 成品网站推广渠道有哪些方式
  • 男女性做那个视频网站查域名注册详细信息查询
  • 网站策划的内容html制作网站
  • 小程序定制公司哪里有seo营销培训咨询
  • 个人做电子商务网站深圳网站seo地址
  • python工程打包供网站开发调用平台营销
  • 网站是不是要用代码做百度竞价是什么
  • 做网站最重要的是什么西安seo优化培训机构
  • 一个网站怎么做软件好用日照seo公司
  • 人民法院公告网失信人名单seo索引擎优化
  • 深圳设计装修公司哪家好seo推广需要多少钱
  • 有没有做租赁的网站电商的运营模式有几种