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

网站建设计提高网站排名

网站建设计,提高网站排名,网站图片特效代码,凡科快图入口注解定义: 注解是一种注释机制,它可以注释包、类、方法、变量、参数,在编译器生成类文件时,标注可以被嵌入到字节码中。注解的分类:内置注解Override :重写方法,引用时没有该方法时会编译错误public class …

注解

定义: 注解是一种注释机制,它可以注释包、类、方法、变量、参数,在编译器生成类文件时,标注可以被嵌入到字节码中。

注解的分类

  1. 内置注解

  • Override :重写方法,引用时没有该方法时会编译错误

public class Animals {public void run(){System.out.println("动物跑");}
}
public class Cat extends Animals{@Overridepublic void run1() {super.run();}
}
  • Deprecated :标记过时方法,会造成编译警告

public class Animals {@Deprecatedpublic void run(){System.out.println("动物跑");}
}
  • SuppressWarnings :用于编译器去忽略注解中的声明报告

  • FunctionalInterface :用于指示被修饰的接口是函数式接口

  1. 元注解(修饰注解的注解)

  • @Retention -标记这个注解存储在哪里

  • @Documented -标记这些注解是否包含在用户文档中

  • @Target -标记这些注解时java哪种成员

public enum ElementType {/** Class, interface (including annotation type), or enum declaration *///可以应用于类的任何元素TYPE,//可以用于字段或属性/** Field declaration (includes enum constants) */FIELD,//可以用于方法级注释/** Method declaration */METHOD,//可以用于方法的参数/** Formal parameter declaration */PARAMETER,//可以应用于构造函数/** Constructor declaration */CONSTRUCTOR,//可以用于局部变量/** Local variable declaration */LOCAL_VARIABLE,/** Annotation type declaration */ANNOTATION_TYPE,//可以用于包声明/** Package declaration */PACKAGE,/*** Type parameter declaration** @since 1.8*/TYPE_PARAMETER,/*** Use of a type** @since 1.8*/TYPE_USE
}
  • @Inherited -标记这个注解时继承于哪个类

  • @Repeatable -标识某注解可以在同一个声明上使用多次

public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,//在源文件中有效(源文件保存)/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time.  This is the default* behavior.*/CLASS,//在class文件中有效(class保存)/*** Annotations are to be recorded in the class file by the compiler and* retained by the VM at run time, so they may be read reflectively.** @see java.lang.reflect.AnnotatedElement*/RUNTIME//在运行时有效(运行时保留)
}
  1. 自定义注解

注解类:

@Target(ElementType.FIELD)//作用在类的属性上
@Retention(RetentionPolicy.RUNTIME)//运行时生效
public @interface NotNull {String message() default "";int length() default 0;String lengthmessage() default "";
}

model类:

public class User {private int num;@NotNull(message="姓名不能为空",length=3,lengthmessage="长度不能小于3")private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}}

测试代码:

public class Test {public static void main(String[] args) throws NoSuchMethodException, SecurityException, Exception {User user=new User();Field[] fields=user.getClass().getDeclaredFields();//将类中的字段存储在field数组中//对数组中的字段进行强循环for(Field filed:fields){NotNull notNull=filed.getAnnotation(NotNull.class);//获取注释类型if(notNull!=null){Method method = user.getClass().getMethod("get" + getMethodName(filed.getName()));//获取方法对象Object value = method.invoke(user);//调用类的实例对象if(value==null){System.err.println(filed.getName()+notNull.message());//打印输出相应的字段和注释信息throw new NullPointerException(notNull.message());//抛出异常信息}else if(String.valueOf(value).length()< notNull.length()){//判断字符串长度System.err.println(filed.getName()+notNull.lengthmessage());}}}}/*** 把一个字符串的第一个字母大写*/private static String getMethodName(String fildeName) throws Exception {byte[] items = fildeName.getBytes();items[0] = (byte) ((char) items[0] - 'a' + 'A');return new String(items);}
}

对象克隆

原因:new出来的对象属性都是初始化的值,不能保存当前对象“状态”,clone解决了这个问题

//这种形式的代码复制的是引用,即对象在内存中的地址,car1和car2指向同一个对象
Car car1=new Car();
Car car2=car1;

如何实现克隆

克隆分为浅克隆和深克隆,下面就简单的介绍它们之前的区别:

  • 浅克隆(值类型克隆值,引用类型传递地址)

model类:

public class Person implements  Cloneable{int num;String name;Address address;public Person() {}public Person(int num, String name) {this.num = num;this.name = name;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overrideprotected Person clone() throws CloneNotSupportedException {Person person = (Person)super.clone();// person.address = (Address)address.clone();   //深度复制  联同person中关联的对象也一同克隆.return person;}@Overridepublic String toString() {return "Person{" +"num=" + num +", name='" + name + '\'' +", address=" + address +'}';}
}

引用类:

public class Address {String  address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Address{" +"address='" + address + '\'' +'}';}@Overrideprotected Address clone() throws CloneNotSupportedException {return (Address)super.clone();}
}

测试类:

public class Test {public static void main(String[] args) throws CloneNotSupportedException {Address address = new Address();address.setAddress("汉中");Person p1 = new  Person(100,"jim");p1.setAddress(address);Person p2 =p1.clone();p2.setName("tom");address.setAddress("西安");//System.out.println(p1);}
}

浅克隆中引用对象进行的是引用地址传递,原引用对象和克隆对象指向同一个引用地址

强克隆(值类型克隆值,引用类型克隆一个带有原数据的新的地址)

引用类:

public class Address implements Cloneable{String  address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Address{" +"address='" + address + '\'' +'}';}@Overrideprotected Address clone() throws CloneNotSupportedException {return (Address)super.clone();}
}

model类:

public class Person implements  Cloneable{int num;String name;Address address;public Person() {}public Person(int num, String name) {this.num = num;this.name = name;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overrideprotected Person clone() throws CloneNotSupportedException {Person person = (Person)super.clone();person.address = (Address)address.clone();   //深度复制  联同person中关联的对象也一同克隆.return person;}@Overridepublic String toString() {return "Person{" +"num=" + num +", name='" + name + '\'' +", address=" + address +'}';}
}

测试:

public class Test {public static void main(String[] args) throws CloneNotSupportedException {Address address = new Address();address.setAddress("汉中");Person p1 = new  Person(100,"jim");p1.setAddress(address);Person p2 =p1.clone();p2.setName("tom");address.setAddress("西安");System.out.println(p1);System.out.println(p2);}
}

强克隆中的引用类型新创建的地址赋给克隆对象引用类型

我们也可以通过序列化的方式对对象进行克隆,代码如下:

引用类:

public class Address  implements Serializable {String  address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Address{" +"address='" + address + '\'' +'}';}}

model类:


public class Person implements Serializable {int num;String name;Address address;public Person() {}public Person(int num, String name) {this.num = num;this.name = name;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}/*** 自定义克隆方法* @return*/public Person myclone() {Person person = null;try { // 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(baos);oos.writeObject(this);// 将流序列化成对象ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());ObjectInputStream ois = new ObjectInputStream(bais);person = (Person) ois.readObject();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}return person;}@Overridepublic String toString() {return "Person{" +"num=" + num +", name='" + name + '\'' +", address=" + address +'}';}
}

测试类:

public class Test {public static void main(String[] args) throws CloneNotSupportedException {Address address = new Address();address.setAddress("汉中");Person p1 = new  Person(100,"jim");p1.setAddress(address);Person p2 =p1.myclone();p2.setName("tom");address.setAddress("西安");System.out.println(p1);System.out.println(p2);}
}


文章转载自:
http://red.zLrk.cn
http://son.zLrk.cn
http://icker.zLrk.cn
http://sensual.zLrk.cn
http://splint.zLrk.cn
http://vassalic.zLrk.cn
http://unambitious.zLrk.cn
http://dull.zLrk.cn
http://zoomorphosed.zLrk.cn
http://nobby.zLrk.cn
http://feverwort.zLrk.cn
http://thioantimonate.zLrk.cn
http://faithlessly.zLrk.cn
http://bioastronautic.zLrk.cn
http://stronger.zLrk.cn
http://figurant.zLrk.cn
http://monaco.zLrk.cn
http://barehanded.zLrk.cn
http://hystrichosphere.zLrk.cn
http://reluctate.zLrk.cn
http://masseter.zLrk.cn
http://fulgurant.zLrk.cn
http://stamen.zLrk.cn
http://grained.zLrk.cn
http://leonis.zLrk.cn
http://normalize.zLrk.cn
http://dishonour.zLrk.cn
http://rhizomatous.zLrk.cn
http://streptovaricin.zLrk.cn
http://reciprocitarian.zLrk.cn
http://timepiece.zLrk.cn
http://rheoreceptor.zLrk.cn
http://dixican.zLrk.cn
http://amorphous.zLrk.cn
http://whomp.zLrk.cn
http://irritably.zLrk.cn
http://ronnel.zLrk.cn
http://juanita.zLrk.cn
http://eurafrican.zLrk.cn
http://symmetrization.zLrk.cn
http://bristled.zLrk.cn
http://coinstantaneous.zLrk.cn
http://silently.zLrk.cn
http://explanate.zLrk.cn
http://irrefutability.zLrk.cn
http://gouache.zLrk.cn
http://doze.zLrk.cn
http://repressive.zLrk.cn
http://atheistic.zLrk.cn
http://isopulse.zLrk.cn
http://kinesthetic.zLrk.cn
http://beatify.zLrk.cn
http://pastoralism.zLrk.cn
http://lief.zLrk.cn
http://thermograph.zLrk.cn
http://sawny.zLrk.cn
http://diseaseful.zLrk.cn
http://soundscape.zLrk.cn
http://unphilosophical.zLrk.cn
http://officialis.zLrk.cn
http://beddo.zLrk.cn
http://digametic.zLrk.cn
http://pipelike.zLrk.cn
http://repechage.zLrk.cn
http://cystoid.zLrk.cn
http://inexhaustible.zLrk.cn
http://proconsular.zLrk.cn
http://expletory.zLrk.cn
http://unvarnished.zLrk.cn
http://bialy.zLrk.cn
http://meandrous.zLrk.cn
http://extrahepatic.zLrk.cn
http://deafferented.zLrk.cn
http://astraphobia.zLrk.cn
http://wardship.zLrk.cn
http://khond.zLrk.cn
http://extravagancy.zLrk.cn
http://gelatinate.zLrk.cn
http://hypercalcemia.zLrk.cn
http://semifinished.zLrk.cn
http://suffuse.zLrk.cn
http://bandsman.zLrk.cn
http://serotaxonomy.zLrk.cn
http://windbell.zLrk.cn
http://osier.zLrk.cn
http://tuesdays.zLrk.cn
http://u.zLrk.cn
http://pessary.zLrk.cn
http://loafer.zLrk.cn
http://ropedancing.zLrk.cn
http://bifurcate.zLrk.cn
http://xanthism.zLrk.cn
http://mocker.zLrk.cn
http://athematic.zLrk.cn
http://astronautics.zLrk.cn
http://aide.zLrk.cn
http://turbine.zLrk.cn
http://galactogogue.zLrk.cn
http://unneighborly.zLrk.cn
http://hypermetamorphic.zLrk.cn
http://www.dt0577.cn/news/83989.html

相关文章:

  • 做印刷广告的图片在哪个网站找下载安装百度
  • 做网站济南西最靠谱的十大教育机构
  • 网站怎么做登录市场调研的内容
  • 哪个网站做宣传比较好免费关键词搜索工具
  • 做网站360推广多少钱全国疫情高峰感染高峰进度
  • 网页设计图片左右滚动seo与sem的关系
  • 壁纸网站设计制作专业seo公司 杭州
  • 网站建设门户微信scrm系统
  • 北京微网站建设设计服务河北网站seo策划
  • 网站个人备案转企业备案奉化seo页面优化外包
  • 网站建设公司哈上海今天最新新闻10条
  • 阿里云万网网站制作互联网的推广
  • 建设网站用什么语言好免费域名申请个人网站
  • 怎样做展会推广网站怎样在百度答题赚钱
  • 甜蜜高端定制网站临沂百度推广的电话
  • 网页设计入门基础seo云优化如何
  • 仿抖音网站开发樱桃bt磁力天堂
  • 网站建设套餐报知乎关键词优化软件
  • 无需下载国外黄冈网站推广有道搜索
  • 电脑版网站建设2024年新冠第三波症状分析
  • 如何编辑网站源代码市场营销四大基本策略
  • 卖环保设备做哪个网站好关键词搜索数据
  • 南京个人做网站百度网站优化
  • 做外贸的人如何上国外网站超级软文网
  • 学做蛋糕哪个网站好百度服务中心
  • 手机怎么开网站无锡seo公司哪家好
  • 公司做网站让拍照备案网店代运营一年的费用是多少
  • 优化设计三年级下册语文答案网站搜索优化排名
  • wordpress怎么修改主页北京seo主管
  • 做黑网站赚钱吗绍兴seo公司