ArrayList、Hashcode分析
java.util.Collection;import
publicclass
main(String[] args) {
new
new
new
new
new
out
}
" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align="left" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align="left" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align="left" font="" style="mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'" face="宋体" font="" size="2" font="" font="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align="left" font="" face="">java.io.FileInputStream;import
java.util.Collection;import
publicclass
main(String[] args) Exception {
new"myconfig.properties"
new
"className"
//Collection collection=new HashSet(); Point p1= Point(3,3); Point p2= Point(5,2); Point p3= Point(6,7); Point p4= Point(3,3); collection.add(p1); collection.add(p2); collection.add(p3); collection.add(p4); collection.add(p4); collection.add(p1); System..println(collection.size()); }
25、用类加载器的方式管理资源和配置文件
第一钟:
InputStream ips=ReflectTest2.class.getResourceAsStream("myconfig.properties"); 第三种:class"good/myconfig.properties"
26、内省和JavaBean:
1、 get()和set()方法属性,如果第二个字母小写,则第一个字母大写。书写美观好看
2、 JavaBean的实例对象被称为值对象(Vaule Object 简称 VO)
3、 对JavaBean的简单内省操作:实例
good;
java.beans.IntrospectionException;import
java.lang.reflect.InvocationTargetException;import
publicclass
main(String[] args) Exception {
new
"x"
out
outx
setMethodValue(Point pt1, String propertyName, Object setValue) IntrospectionException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pds= PropertyDescriptor(propertyName, pt1.getClass()); Method setMethodX=pds.getWriteMethod(); setMethodX.invoke(pt1, setValue); }
privatestatic
throws
new
return
}privatestatic
throws
/*PropertyDescriptor pd=new PropertyDescriptor(propertyName, pt1.getClass()); Method getMethodX=pd.getReadMethod(); Object retValue=getMethodX.invoke(pt1);*/ BeanInfo beaninfo=Introspector.getBeanInfo(pt1.getClass()); PropertyDescriptor[] pds=beaninfo.getPropertyDescriptors(); Object retValue=; (PropertyDescriptor pd:pds){ (pd.getName().equals(propertyName)){ Method getMethodX=pd.getReadMethod(); retValue=getMethodX.invoke(pt1); ; } } retValue; }
BeanUtils(跟PropertyUtils(跟具体的数据类型)的区别import
org.apache.commons.beanutils.PropertyUtils;
JavaBeanUtilsTest {
publicstaticvoidthrows
Point pt1= Point(5,9); System..println(BeanUtils.getProperty(pt1, )); BeanUtils.setProperty(pt1, , 34); System..println(BeanUtils.getProperty(pt1, )); BeanUtils.setProperty(pt1, , ); System..println(BeanUtils.getProperty(pt1, )); PropertyUtils.setProperty(pt1, , 88); System..println(PropertyUtils.getProperty(pt1, )); }
28、注解
注解种类:
1】 压缩警告:@SuppressWarnings
2】 过时注解:@Deprecated
3】 覆盖注解:@Override
注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,以后Java编译器,开发工具和其他程序可以用反射来了解该类及各种元素有无任何标记,有就去做相应的事情,标记可以加在包、类、字段、方法、方法的参数及局部变量上。
@Retenition元注解,三种取值:RetetionPolicy.SOURCE、RetetionPolicy.CLASS、RetetionPolicy.RUNTIME,分别对应:Java源文件->class文件->内存中的字节码
@Override和@SuppressWarnings属性值为RetetionPolicy.SOURCE
@Deprecated对应RetetionPolicy.RUNTIME
注解基本属性:
1】 注解的类
()publicclass
() main(String[] args) {
ifclassItcastAnnocationclass
ItcastAnnocationclassItcastAnnocationclass
out
out
out
outlength
out
out
}import
java.lang.annotation.Target;@RetentionRUNTIME
({ElementType.,ElementType.})public@interfaceItcastAnnocation
String color() ; String value(); [] arrayAttr() {34,23,4552}; good.TrafficLamp.Traffic lamp() good.TrafficLamp.Traffic.; annotationAttr() ();}<FONT face=""">