JDK1.5新特性
一,静态导入
package com.zsj.test;
import static java.lang.Math.max;
public class StaticImport {
public static void main(String[] args) {
System.out.println(max(3.0, 5.0));
}
}
二、动态参数
package com.zsj.test;
public class DynamicArgs {
//overload vs override
public static void main(String[] args) {
System.out.println(add(0, 2, 3, 5));
}
public static int add(int x,int ... args){
int sum = 0;
for(int i=0;i
sum+=args[i];
}
return sum;
}
}
三、增强for循环
package com.zsj.test;
public class StrongFor {
public static void main(String[] args) {
int [] as = null;
int sum = 0;
for(int arg:as){
sum += arg;
}
}
}
四、装箱与拆箱
package com.zsj.test;
public class AutoBox {
public static void main(String[] args) {
Integer iobj = 3;//装箱
System.out.println(iobj+12);//拆箱
Integer i1 = 13;
Integer i2 = 13;
System.out.println(i1==i2);//true
Integer i3 = 137;
Integer i4 = 137;
System.out.println(i3==i4);//false
//-128~127才可以装箱,存在共享池里。此也是一种设计模式(享元模式flyweight)多个小对象装成一个对象
Integer i5 = Integer.valueOf(3);
Integer i6 = Integer.valueOf(3);
System.out.println(i5==i6);//true
}
}
五、枚举
1、模拟枚举
package com.zsj.test;
public class WeekDay {
private WeekDay(){}
public final static WeekDay SUN = new WeekDay();
public final static WeekDay MON = new WeekDay();
public WeekDay nextDay(){
if(this==SUN){
return MON;
}else{
return SUN;
}
}
public String toString() {
return this==SUN?"SUN":"MON";
}
}
package com.zsj.test;
public class Enumeration {
public static void main(String[] args) {
WeekDay weekDay = WeekDay.SUN;
System.out.println(weekDay.nextDay());
}
}
2、模拟枚举改善
package com.zsj.test;
public abstract class WeekDay {
private WeekDay(){}
public final static WeekDay SUN = new WeekDay(){
public WeekDay nextDay() {
return null;
}
};
public final static WeekDay MON = new WeekDay(){
public WeekDay nextDay() {
return null;
}
};
//用抽象方法定义nextDay就将大量的if..else语句转移成一个个独立的类
public abstract WeekDay nextDay();
//public WeekDay nextDay(){
//if(this==SUN){
//return MON;
//}else{
//return SUN;
//}
//}
public String toString() {
return this==SUN?"SUN":"MON";
}
}
3、枚举的入门
package com.zsj.test;
public class Enumeration {
public static void main(String[] args) {
WeekDay1 weekDay1 = WeekDay1.MON;
System.out.println(weekDay1);//MON[自动实现了toString()方法]
System.out.println(WeekDay1.valueOf("SUN"));//SUN
}
public enum WeekDay1{
SUN,MON,TUE,WEN,THI,FRI,SAT
}
}
4、枚举的构造方法
package com.zsj.test;
public class Enumeration {
public static void main(String[] args) {
WeekDay1 weekDay1 = WeekDay1.MON;
System.out.println(weekDay1);//MON[自动实现了toString()方法]
System.out.println(WeekDay1.valueOf("SUN"));//SUN
}
public enum WeekDay1{
SUN(1),//调用第二个构造方法
MON(),TUE,WEN,THI,FRI,SAT;
private WeekDay1(){//构造方法只能是私有的
System.out.println("construct first");
};
private WeekDay1(int day){//构造方法只能是私有的
System.out.println("construct second");
};
}
}
5、定义抽象方法的枚举类
(一)public enum Light{
RED{ //实现的子类
public Light nextLamp() {
return GREEN;
}
},GREEN{
public Light nextLamp() {
return YELLOW;
}
},YELLOW{
public Light nextLamp() {
return GREEN;
}
};
public abstract Light nextLamp();
}
(二)public enum Light{
RED(5){ //实现的子类
public Light nextLamp() {
return GREEN;
}
},GREEN(6){
public Light nextLamp() {
return YELLOW;
}
},YELLOW(3){
public Light nextLamp() {
return GREEN;
}
};
public abstract Light nextLamp();
private int time;
private Light(int time){
this.time = time;
}
}
(三)、枚举只有一个成员时,可以作为一个单例的实现方式
六、反射
1、package com.zsj.test;
public class ReflectTest{
public static void main(String[] args) throws Exception{
//获得各类型的字节码
String str1 = "abc";
Class cls1 = str1.getClass();
Class cls2 = String.class;
Class cls3 = Class.forName("java.lang.String");
System.out.println(cls1==cls2);//true
System.out.println(cls2==cls3);//true
System.out.println(cls1.isPrimitive());//false是否为基本类型
System.out.println(int.class.isPrimitive());//true
System.out.println(int.class==Integer.class);//false
System.out.println(int.class==Integer.TYPE);//true
System.out.println(int[].class.isPrimitive());//fasle
System.out.println(int[].class.isArray());//true
}
}
2、反射就是将java类中各个成分映射成相应的类
七、内省(Introspector)—》javabeanà特殊的java类
package com.zsj.test;
import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class Introspector {
public static void main(String[] args) throws Exception {
Point point = new Point(3,5);
String propertyName = "x";
PropertyDescriptor descriptor = new PropertyDescriptor(propertyName,point.getClass());
Method methodX = descriptor.getReadMethod();
Object getX = methodX.invoke(point);
System.out.println(getX);//3
Method methodX1 = descriptor.getWriteMethod();
Object setX = methodX1.invoke(point,7);
System.out.println(setX);//7
//BeanInfo beanInfo = java.beans.Introspector.getBeanInfo(Point.class);
//PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
//if(PropertyDescriptor pdd: pds){
//if(pdd.getName().equals(propertyName)){
//Method methodX = descriptor.getReadMethod();
//Object getX = methodX.invoke(point);
//}
//}
}
}
BeanUtils
Java7新特性如Map map = {name:”sss”,age:”dfdf”};
八、注解(jdk5新特性)
hashSet不能加入重复的对象,然后是通过equals和hashCode判断的
九、泛型(jdk1.5新特性)
package com.zsj.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class Gerneric {
public static void main(String[] args) {
HashMap
map.put("asas",23);
map.put("ds", 12);
Set
for(Entry
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
只有引用类型才能作为泛型方法的实际调用
在返回值之前定义一类型