`
tempsitegoogle
  • 浏览: 865023 次
文章分类
社区版块
存档分类
最新评论

黑马程序员__javaweb--java基础加强

 
阅读更多

---------------------- android培训java培训、期待与您交流! ----------------------

JDK5.0的新特性:

静态导入

自动装箱、拆箱

增强for循环

可变参数

枚举

泛型

元数据

断点:

f5step into

f6step over

f7step return

ctrl+1:快速修复

ctrl+shift+x:变大写

ctrl+shift+y:变小写

ctrl+alt+向下键:复制行

alt+向上、向下键:向上、向下移

ctrl+T:查看继承关系

ctrl+shift+T:查看源代码

ctrl+shift+L:查看所有快捷键

Junit对类进行测试:要加注解@Test@before{可以用于初始化资源}@after{销毁资源}

断言:Assert.

drop to frame: 跳到当前方法第一行

resume:跳到下一个端点(如果没有下一个,则运行完整个程序)

watch:观察变量或表达式的值

断点注意的问题:

1、 断点调试完成后,要在breakpoints视图中清除所有断点;

2、 断点调试完成后,一定要记得结束运行断点的jvm

取集合值的几种方式:

publicclass javaweb01 {

@Test

publicvoid test(){

Map map = new LinkedHashMap<String, String>();

map.put("1", "sdhdh");

map.put("2", "dqd");

map.put("3", "fag");

/* Set set = map.keySet();

Iterator it=set.iterator();

while(it.hasNext()){

String key=(String) it.next();

String value=(String) map.get(key);

System.out.println(key+"="+value);

}*/

Set set = map.keySet();

for(Object obj:set){

String key=(String) obj;

String value=(String) map.get(key);

System.out.println(key+"="+value);

}

}

@Test

publicvoid test2(){

Map map = new LinkedHashMap<String, String>();

map.put("1", "sdhdh");

map.put("2", "dqd");

map.put("3", "fag");

/*Set set=map.entrySet();

Iterator it=set.iterator();

while(it.hasNext()){

Map.Entry entry=(Entry) it.next();

String key=(String) entry.getKey();

String value=(String) entry.getValue();

System.out.println(key+"="+value);

}*/

for(Object obj:map.entrySet()){

Map.Entry entry=(Entry) map.entrySet();

String key=(String) entry.getKey();

String value=(String) entry.getValue();

System.out.println(key+"="+value);

}

}

使用增强for循环,只适合取数据,要修改数组或集合中的数据,要用传统方式!

枚举类特性:

枚举也是一种特殊形式的java

枚举类中声明的每一个枚举值代表枚举类的一个实例对象

java中的普通类一样,在声明枚举类时,也可以声明属性,方法和构造函数,但枚举类的构造函数必须是私有的

枚举类也可以实现接口,或继承抽象类

JDK5还扩展了switch语句,它除了可以接收intbytecharshort外,还可以接收一个枚举

若枚举类只有一个枚举值,则可以当做单例设计模式

枚举的常用方法:name()--返回枚举名,ordinal()--返回枚举位置号,valueof()--将字符串转成枚举值,values()--用于返回枚举的所有枚举值,返回一个枚举类型的数组!

反射:反射就是加载类,并解剖出类的各个组成部分(成员变量、方法、构造方法)

主函数的反射:

反射类的构造函数,建立对象、函数(主函数)、字段

Class clazz=Class.forName(“ ”);

Method method=clazz.getMethod(“main”,String[ ].class);

method.invoke(“null”,(Object[ ])(new String(“aaa”,”ghj”)));//拆就让你拆个够

//method.invoke(“null”,(Object)(new String(“aas”,”dsf”)));方老师的话说叫“欺骗”

反射字段:

Person p=new Person( );

Class clazz=Class.forName(“ ”);

Field f=clazz.getField(“name”);

String name=(String)f.get(p);

System.out.println(name);

内省操作:

实例:

publicclass Test1 {

@Test

publicvoid test1() throws Exception {

BeanInfo info=

Introspector.getBeanInfo(Person.class,Object.class);

PropertyDescriptor[] pds=info.getPropertyDescriptors();

for(PropertyDescriptor pd:pds){

System.out.println(pd.getName());

}

}

@Test

publicvoid test2()throws Exception{

Person p=new Person();

PropertyDescriptor pd=new PropertyDescriptor("age", Person.class);

//pd.getPropertyType();获得属性类型

Method method=pd.getWriteMethod();

method.invoke(p, 45);

System.out.println(p.getAge());

}

}

BeanUtils—支持八种基本数据类型(需要的是bytecharshortintlongfloatdoubleboolean可以直接转)

自己注册转换器:

publicclass Demo1 {

@Test

publicvoid test1() throws IllegalAccessException, InvocationTargetException{

Person p=new Person();

BeanUtils.setProperty(p, "name","xiaotie");

System.out.println(p.getName());

}

@Test

publicvoid test2() throws IllegalAccessException, InvocationTargetException{

Person p=new Person();

String name="TangKang";

String password="534946910";

String age="21";

String birthday="1990-10-10";

ConvertUtils.register(new Converter(){

@Override

public Object convert(Class type, Object value) {

// TODO Auto-generated method stub

if(value==null){

returnnull;

}

if(!(value instanceof String)){

thrownew ConversionException("只支持字符串类型的转换");

}

String str=(String)value;

if(str.trim().equals("")){

returnnull;

}

SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");

try {

return sd.parse(str);

} catch (ParseException e) {

thrownew RuntimeException(e);//异常链不能断;

}

}

}, Date.class);

BeanUtils.setProperty(p, "name", name);

BeanUtils.setProperty(p, "password", password);

BeanUtils.setProperty(p, "age", age);

BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());

System.out.println(p.getPassword());

System.out.println(p.getAge());

System.out.println(p.getBirthday().toLocaleString());

}

}

---------------------- android培训java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics