------- android培训、java培训、期待与您交流! ----------
一、静态导入
package
com.JDK5;
/**
* import static 类下的静态方法
*
* 为了调用时不写类名
*/
import
static
java.lang.Math.max ;
import
static
java.lang.Math.*;
public
class
ImportStaticDemo {
public
static
void
main(String[] args) {
//System.out.println(Math.max(3, 6));
System.
out
.println(max(3, 6));
}
}
|
二、可变参数
特点:
只能出现在参数列表最后
位于变量类型和变量名之间,前后有无空格都可以
调用可变参数方法时,编译器为该可变参数隐含创建一个数组,在方法体中以数组的形式访问可变参数
调用可变参数方法时,编译器为该可变参数隐含创建一个数组,在方法体中以数组的形式访问可变参数
package
com.JDK5;
public
class
VarableParameter {
public
static
void
main(String[] args) {
//
TODO
Auto-generated method stub
System.
out
.println(add(2,3,4));
}
public
static
int
add(
int
x,
int
... args){
int
sum = x;
for
(
int
i=0;i<args.
length
;i++){
sum += args[i];
}
return
sum;
}
}
|
三、for循环增强
for(type 变量名 : 集合变量名)
for
(
int
arg : args){
sum += arg;
}
特点:使用增强for循环遍历数组或集合时,无需指定长度,也无需根据索引来访问元素
注意:增强for循环只能遍历数组或集合,不能改变数组或集合的值
弊端:不能选择性进行循环,例如上面for语句不能进行下标是偶数的sum。
四、基本数据类型的自动装箱和拆箱
package
com.itcast.day1;
public
class
AtuoBox {
/**
*
@param
args
*/
public
static
void
main(String[] args) {
//自动装箱
Integer iObje = 3;
//自动拆箱
System.
out
.println(iObje + 2);
Integer i1 = 13;
Integer i2 = 13;
//-128~127之间,缓存到池,
System.
out
.println(i1 == i2);//true
}
}
|
五、枚举
是一种特殊的类,其中的每个元素都是该类的一个实例对象,例如可以调用WeekDay.SUN.getClass().getName和WeekDay.class.getName().
package
com.itcast.day1;
import
java.util.Date;
public
class
EnumTest {
/**
*
@param
args
*/
public
static
void
main(String[] args) {
WeekDay1 weekDay = WeekDay1.
SUN
;
System.
out
.println(weekDay.toString());
WeekDay wk = WeekDay.
SUN
;
System.
out
.println(wk);
System.
out
.println(wk.name());
System.
out
.println(wk.ordinal());
System.
out
.println(wk.valueOf (
"SUN"
).toString());
System.
out
.println(wk.values ().
length
);
System.
out
.println(wk.
SUN
.getClass().getName());
System.
out
.println(wk.getClass().getName());
}
public
enum
WeekDay{
SUN
,
MON
,
TUE
,
WED
,
THI
,
FRI
,
SAT
;
}
}
|
枚举就相当于一个类,其中也可以定义构造方法、成员变量、普通方法和抽象方法。
枚举元素必须位于枚举体中的最开始部分,枚举元素列表的后要有分号与其他成员分隔。把枚举中的成员方法或变量等放在枚举元素的前面,编译器报告错误。
带构造方法的枚举,构造方法必须定义成私有的
如果有多个构造方法,该如何选择哪个构造方法?
枚举元素MON和MON()的效果一样,都是调用默认的构造方法。
带方法的枚举
定义枚举TrafficLamp
实现普通的next方法
实现抽象的next方法:每个元素分别是由枚举类的子类来生成的实例对象,这些子类采用类似内部类的方式进行定义。
增加上表示时间的构造方法
枚举只有一个成员时,就可以作为一种单例的实现方式。
带构造方法的枚举,构造方法必须定义成私有的
如果有多个构造方法,该如何选择哪个构造方法?
枚举元素MON和MON()的效果一样,都是调用默认的构造方法。
带方法的枚举
定义枚举TrafficLamp
实现普通的next方法
实现抽象的next方法:每个元素分别是由枚举类的子类来生成的实例对象,这些子类采用类似内部类的方式进行定义。
增加上表示时间的构造方法
枚举只有一个成员时,就可以作为一种单例的实现方式。
//定义枚举的关键字 enum
public
enum
TrafficLamp{
RED
(30){
@Override
public
TrafficLamp nextLamp() {
//
TODO
Auto-generated method stub
return
GREEN
;
}
},
GREEN
(60){
@Override
public
TrafficLamp nextLamp() {
//
TODO
Auto-generated method stub
return
YELLOW
;
}
},
YELLOW
(5){
@Override
public
TrafficLamp nextLamp() {
//
TODO
Auto-generated method stub
return
RED
;
}
};
public
abstract
TrafficLamp nextLamp();
private
int
time
;
private
TrafficLamp(
int
time){
this
.
time
=time;
}
}
测试的方法:
//当前灯
TrafficLamp red = TrafficLamp.
RED
;
System.
out
.println(red +
"--->"
+red.nextLamp());
|
六、注解
注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记。以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。
三个基本注解
@Overrride,限定方法一定要覆盖父类的方法。
@Deprecated,标记元素已过时,编译器编译时会提示。
@SuppressWarnings,告诉编译器某种信息
package
com.itcast.day2;
public
class
AnnotationTest {
/**
*
@param
args
*/
@SuppressWarnings
(
"deprecation"
)
//告诉编译器不提示过期
//注解,告诉编辑器某种信息,一个注解就是一个类
public
static
void
main(String[] args) {
System.
AnnotationTest.
}
@Deprecated
//过时了
public
static
void
System.
out
.println(
"hi,传智播客"
);
}
}
|
自定义注解及应用:
格式:public @interface 类名{}
把注解加载一个类上:
@类名public class AnnotationTest{};
可以利用反射测试MainCalss的定义上是否有某注解:
AnnotationTest.
class
.isAnnotationPresent(
ItcastAnnotation
.
class
)
@Retention元注解:注解的生命周期,有三个值:@RetetionPolicy.SOURCE;@RetetionPolicy.CLASS;
@RetetionPolicy.RUNTIME;
可以为注解增加属性:可以使数组类型的、枚举类型、注解类型等
自定义注解类:
ItcastAnnotation
package com.itcast.day2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import com.itcast.day1.EnumTest; //元注解 说明注解的生命周期 内存运行阶段 //值有三种:RetentionPolicy.RUNTIME,RetentionPolicy.CLASS,RetentionPolicy.SOURCE @Retention(RetentionPolicy.RUNTIME)//默认在class阶段 //@Target(ElementType.METHOD)//把注解保留在方法上 //class的父是type @Target({ElementType.METHOD,ElementType.TYPE})//把注解保留在方法上或者类上 public @interface ItcastAnnotation { //为注解添加属性 String color() default "blue"; String value(); int[] arrayAttr() default {3,4,5}; EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.GREEN; MetaAnnotation annotationAttr() default @MetaAnnotation("sssss"); Class a(); } |
测试类:
AnnotationTest
package
com.itcast.day2;
@ItcastAnnotation
(a=String.
class
,annotationAttr=
@MetaAnnotation
(
"zzz"
),color=
"red"
,value=
"123"
,arrayAttr=1)
//@ItcastAnnotation(" xsd")//只有value属性需要设置的时候,才可以直接
public
class
AnnotationTest {
/**
*
@param
args
*/
@SuppressWarnings
(
"deprecation"
)
//告诉编译器不提示过期
//注解,告诉编辑器某种信息,一个注解就是一个类
//@ItcastAnnotation("xsd")
public
static
void
main(String[] args) {
System.
AnnotationTest.
//如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。
if
(AnnotationTest.
class
.isAnnotationPresent(
ItcastAnnotation
.
class
)){
// 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。
ItcastAnnotation
annotation = (
ItcastAnnotation
)AnnotationTest.
class
.getAnnotation(
ItcastAnnotation
.
class
);
System.
out
.println(annotation);
System.
out
.println(annotation.color());
System.
out
.println(annotation.value());
System.
out
.println(annotation.arrayAttr().
length
);
System.
out
.println(annotation.lamp());
System.
out
.println(annotation.lamp().nextLamp().name());
System.
out
.println(annotation.annotationAttr().value());
System.
out
.println(annotation.a());
}
}
@Deprecated
//过时了
public
static
void
System.
out
.println(
"hi,传智播客"
);
}
}
|
七、泛型(编译期)
为什么引入泛型?
1.可以统一集合中的数据类型
2.可以减少强制类型转换.
泛型的优点和缺点?
优点:统一类型,减少强制转换.
缺点:只能存储一种类型.
1.可以统一集合中的数据类型
2.可以减少强制类型转换.
泛型的优点和缺点?
优点:统一类型,减少强制转换.
缺点:只能存储一种类型.
查看文档时,
只要带有<>的类或者接口,都属于带有类型参数的类或者接口,在使用这些类或者接口时,必须给<>中传递一个具体的引用数据类型。
泛型在程序中的体现:
1、定义在类上。
2、当方法操作的引用数据类型不确定时,定义在方法上
3、静态方法上的泛型:
静态方法无法访问类上定义的泛型。如果静态方法操作的引用数据类型不确定的时候,必须要将泛型定义在方法上。
4、定义在接口上
泛型中的通配符:?,
当操作类型时,不需要使用类型的具体功能时,只使用Object类中的功能。那么可以用 ? 通配符来表未知类型。
上限:
?extends E:可以接收E类型或者E的子类型对象。可以用于往集合中添加元素
下限:?super E:
可以接收E类型或者E的父类型对象。可以用于冲集合中获取元素
例:
package
com.itcast.day2;
import
java.lang.reflect.InvocationTargetException;
import
java.util.*;
public
class
GenericTest01 {
/**
*
@param
args
*
@throws
Exception
*
@throws
InvocationTargetException
*
@throws
IllegalAccessException
*
@throws
SecurityException
*
@throws
IllegalArgumentException
*/
public
static
void
main(String[] args)
throws
IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, Exception {
ArrayList collection =
new
ArrayList();
collection.add(1);
collection.add(1L);
collection.add(
"abc"
);
int
i= (Integer) collection.get(0);
//不确定取到的值,是什么类型,而且需要强制类型转换
System.
out
.println(i);
//编译期会检查泛型,运行时会去掉类信息。
ArrayList<Integer> collection3 =
new
ArrayList();
//collection3.add(" qwe");
collection3.getClass().getMethod(
"add"
, Object.
class
).invoke(collection3,
"qwe"
);
System.
out
.println(collection3.get(0));
}
}
|
Map使用泛型:
package
com.itcast.day2;
import
java.util.*;
public
class
GenericTest03{
public
static
void
main(String[] args){
Map<String,Integer> maps =
new
HashMap<String,Integer>();
//存
maps.put(
"西瓜"
,10);
maps.put(
"苹果"
,9);
maps.put(
"香蕉"
,20);
//遍历
Set<String> keys = maps.keySet();
Iterator<String> it = keys.iterator();
while
(it.hasNext()){
String k = it.next();
Integer v = maps.get(k);
System.
out
.println(k+
"--->"
+v);
}
}
}
|
自定义泛型及应用:
package com.itcast.day2; import java.lang.reflect.*; import java.util.*; import com.itcast.day1.ReflectPoint; public class GenericTest { public static void main(String[] args) throws Exception, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{ //编译期会检查泛型,运行时会去掉类信息。 ArrayList<Integer> collection3 = new ArrayList(); collection3.getClass().getMethod("add", Object.class).invoke(collection3, "qwe"); ArrayList collection4 = new ArrayList<String>(); ArrayList<Object> v = collection4; printCollection(collection3); Class<?> y; Class<String> x = null ;//Class.forName("java.lang.String"); //x = y;错误 y = x; //自动装箱 add(3,4);//int add(3,23.4);//Number add(3,"asdfghjk");//Object swap(new String[]{"abc","bcd","dfg"},1,2); //swap(new int[]{1,2,3,4,5},3,4);只能是引用类型 //因为编译器不会对new int[3]中的int自动拆箱和装箱了, //因为new int[3]本身已经是对象了 Object obj = "abc"; String x3 = autoConvert(obj); GenericDao<ReflectPoint> dao = new GenericDao<ReflectPoint>(); dao.add(new ReflectPoint(3,3)); dao.findById(1); //Vector<Date> v1 = new Vector<Date>(); Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class); //得到参数类型 Type[] types = applyMethod.getGenericParameterTypes(); ParameterizedType pType = (ParameterizedType) types[0]; //原始类型 System.out.println(pType.getRawType()); System.out.println(pType.getActualTypeArguments()[0]); } public static void applyVector(Vector<Date> v1){ } private static <T> T autoConvert(Object obj){ return (T)obj; } //用于数组中两个元素交换位置 private static <T> void swap(T[] a,int i,int j){ T tmp = a[i]; a[i] = a[j]; a[j] = tmp; } private static <T>T add(T x,T y){ System.out.println("<T>"+y); return y; } public static void printCollection(Collection<?> collection){ //collection.add("kil");错误 System.out.println(collection.size()); for(Object obj : collection){ System.out.println(obj+"!!!"); } } } |