3、注:自动封箱和自动解箱只会在必要的情况下执行。
二、静态引用概念:
如:import static java.lang.System.*;
out.println(“a”);
三、新型for循环for—each,用于追求数组与集合的遍历方式统一
1、数组举例:
String[] ss = {“a”,”b”,”c”};
for(String s : ss) { // s:遍历元素类型 ss:要遍历的对象
System.out.println(s);
}
2、集合举例:
List ll = new ArrayList();
for(Object o : ll ){
System.out.println(o);
}
注:凡是实现了java.lang.Iterable接口的类就能用for—each遍历;
四、可变长的参数
一个关于变长参数的例子:
/**********************************************************/ import static java.lang.System.*; public class TestVararg { <span style="white-space:pre"> </span>public static void main(String... args) { <span style="white-space:pre"> </span>m(); <span style="white-space:pre"> </span>m("Liucy"); <span style="white-space:pre"> </span>m("Liucy","Hiloo"); <span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>static void m(String... s) { <span style="white-space:pre"> </span>//s可以看作是一个字符串数组String[] s <span style="white-space:pre"> </span>out.println("m(String...)"); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>static void m(){ <span style="white-space:pre"> </span>out.println("m()"); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>static void m(String s){ <span style="white-space:pre"> </span>out.println("m(String)"); <span style="white-space:pre"> </span>} } /**********************************************************/注:一个方法的参数列表中最多只能又一个变长参数,而且这个变长参数必须是最后一个参数。方法调用时只会在必要时去匹配变长参数。
五、枚举enum
1、定义:枚举是一个具有特定值的类型,对用户来说只能任取其一。对于面向对象来说时一个类的对象已经创建好,用户不能新生枚举对象,只能选择一个已经生成的对象。
2、枚举本质上也是一个类。枚举值之间用逗号分开,以分号结束。
3、枚举分为两种:类型安全的枚举模式和类型不安全的枚举模式
4、枚举的超类是:Java.lang.Enum。枚举的构造方法默认也必须是private并且枚举是一个final类所以不能有子类。
5、一个枚举值实际上是一个公开静态的常量,也是这个类的一个对象。
6、枚举中可以定义抽象方法,其实现在个个枚举值中(匿名内部类的方式隐含继承)
/**********************************************************/ final class Season1{ <span style="white-space:pre"> </span>public static final Season1 SPRING=new Season1("春");
<span style="white-space:pre"> </span>public static final Season1 SUMMER=new Season1("夏"); <span style="white-space:pre"> </span>public static final Season1 AUTUMN=new Season1("秋");
public static final Season1 WINTER=new Season1("冬");
<span style="white-space:pre"> </span>private Season1(){}
<span style="white-space:pre"> </span>String name;
<span style="white-space:pre"> </span>private Season1(String name){ <span style="white-space:pre"> </span>this.name=name; <span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getName(){ <span style="white-space:pre"> </span>return this.name; <span style="white-space:pre"> </span>} } /**********************************************************/ /**********************************************************/
</pre><pre name="code" class="java">enum Season2{ <span style="white-space:pre"> </span>SPRING("春"), <span style="white-space:pre"> </span>SUMMER("夏"), <span style="white-space:pre"> </span>AUTUMN("秋"), <span style="white-space:pre"> </span>WINTER("冬"); <span style="white-space:pre"> </span>String name; <span style="white-space:pre"> </span>Season2(String name){ <span style="white-space:pre"> </span>this.name=name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getName(){ <span style="white-space:pre"> </span>return this.name; <span style="white-space:pre"> </span>} } /**********************************************************/7、一个关于枚举的例子
/**********************************************************/ import static java.lang.System.*; public class TestTeacher { <span style="white-space:pre"> </span>public static void main(String[] args) { <span style="white-space:pre"> </span>for(TarenaTeacher t:TarenaTeacher.values()){
t.teach(); } } } enum TarenaTeacher{ LIUCY("liuchunyang"){ void teach(){out.println(name+" teach UC");}
}, CHENZQ("chenzongquan"){ void teach(){out.println(name+" teach C++");}
}, HAIGE("wanghaige"){ void teach(){out.println(name+" teach OOAD");}
}; String name; TarenaTeacher(String name){ this.name=name; } abstract void teach();
}
六、泛型1、为了解决类型安全的集合问题引入了泛型。
5、泛型的通配符<?>
泛型的通配符表示该集合可以存放任意类型的对象。
static void print( Cllection<?> c ){
for( Object o : c )
out.println(o);
}
6、带范围的泛型通配符
(1)、向下匹配:<? extends Number>
表示该集合元素可以为Number类型及其子类型(包括接口)
(2)、向上匹配:<? super Number>
表示该集合元素可以为Number类型及其父类型
7、泛型方法
在返回值与修饰符之间可以定义一个泛型方法
public static <T,E extends T> void copy (T[] array,Stack<E> sta){…..}
8、不能使用泛型的情况:
(1)、带泛型的类不能成为Throwable类和Exception类的子类
因为cathc()中不能出现泛型。
(2)、不能用泛型来new一个对象
如:T t = new T();
(3)、静态方法不能使用类的泛型,因为静态方法中没有对象的概念。
9、在使用接口的时候指明泛型。
class Student implements Comparable<Student>{…….}
-