荥阳网站建设,推广步骤,郓城做网站,如何自建网站接广告注解定义#xff1a; 注解是一种注释机制#xff0c;它可以注释包、类、方法、变量、参数#xff0c;在编译器生成类文件时#xff0c;标注可以被嵌入到字节码中。注解的分类#xff1a;内置注解Override :重写方法#xff0c;引用时没有该方法时会编译错误public class …注解定义 注解是一种注释机制它可以注释包、类、方法、变量、参数在编译器生成类文件时标注可以被嵌入到字节码中。注解的分类内置注解 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 用于指示被修饰的接口是函数式接口元注解(修饰注解的注解)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//在运行时有效运行时保留
}自定义注解注解类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姓名不能为空,length3,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 usernew User();Field[] fieldsuser.getClass().getDeclaredFields();//将类中的字段存储在field数组中//对数组中的字段进行强循环for(Field filed:fields){NotNull notNullfiled.getAnnotation(NotNull.class);//获取注释类型if(notNull!null){Method method user.getClass().getMethod(get getMethodName(filed.getName()));//获取方法对象Object value method.invoke(user);//调用类的实例对象if(valuenull){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 car1new Car();
Car car2car1;如何实现克隆克隆分为浅克隆和深克隆下面就简单的介绍它们之前的区别浅克隆值类型克隆值引用类型传递地址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://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.hlrtzcj.cn.gov.cn.hlrtzcj.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.jrdbq.cn.gov.cn.jrdbq.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.khxwp.cn.gov.cn.khxwp.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.lhldx.cn.gov.cn.lhldx.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.dighk.com.gov.cn.dighk.com http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.jkbqs.cn.gov.cn.jkbqs.cn http://www.morning.jynzb.cn.gov.cn.jynzb.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.mrlkr.cn.gov.cn.mrlkr.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.bmqls.cn.gov.cn.bmqls.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn