Java基础加强 (四)

2014-11-24 09:56:18 · 作者: · 浏览: 2
/public ProductDaoImpl(){

//super(Product.class);

//}

public List findByCondition(String where) {

return null;

}

}


20、泛型的高级应用——通配符

(1)定义一个方法,接收一个集合,并打印出集合中的所有元素,如下所示:

void print (Collection c) {

for (String e : c) {

System.out.println(e);

}

}


(2)问题:该方法只能打印保存了String对象的集合,不能打印其它集合。通配符用于解决此类问题,方法的定义可改写为如下形式:

void print (Collection< > c) {//Collection< >(发音为:"collection of unknown")

for (Object e : c) {

System.out.println(e);

}

}

(3)此种形式下需要注意的是:由于print方法c参数的类型为Collection< >,即表示一种不确定的类型,因此在方法体内不能调用与类型相关的方法,例如add()方法。

(4)总结:使用 通配符主要用于引用对象,使用了 通配符,就只能调对象与类型无关的方法,不能调用对象与类型有关的方法。


import java.util.ArrayList;

import java.util.Collection;


public class Demo3 {


public static void main(String[] args) {

m1(new ArrayList());

}

//泛型通配符

public static void m1(Collection< > collection){

// collection.add(1);//使用通配符,由于泛型的类型不确定,因此方法内部不能调用与泛型有关的方法

for(Object o:collection)

System.out.println(o);

}

}

21、泛型的高级应用——有限制的通配符

(1)限定通配符的上边界:

正确:Vector< extends Number> x = new Vector();

错误:Vector< extends Number> x = new Vector();

(2)限定通配符的下边界:

正确:Vector< super Integer> x = new Vector();

错误:Vector< super Integer> x = new Vector();

22、Annotation(注解) 概述

(1)从 JDK 5.0 开始, Java 增加了对元数据(MetaData) 的支持, 也就是 Annotation(注解)。

(2)什么是Annotation,以及注解的作用 三个基本的 Annotation:

@Override: 限定重写父类方法, 该注解只能用于方法

@Deprecated: 用于表示某个程序元素(类, 方法等)已过时

@SuppressWarnings: 抑制编译器警告.

package com.itheima.annotation;


import java.util.ArrayList;

import java.util.Date;

import java.util.List;

public class Demo1 {

@SuppressWarnings({"all"})

public static void main(String[] args) {

Date d = new Date();

System.out.println(d.toLocaleString());

List list = new ArrayList();


int i = 10;

mm();

}


@Override

public String toString() {

return super.toString();

}

@Deprecated

public static void mm(){


}

}

(3)Annotation 其实就是代码里的特殊标记, 它用于替代配置文件,也就是说,传统方式通过配置文件告诉类如何运行,有了注解技术后,开发人员可以通过注解告诉类如何运行。在Java技术里注解的典型应用是:可以通过反射技术去得到类里面的注解,以决定怎么去运行类。

(4)掌握注解技术的要点:

如何定义注解

如何反射注解,并根据反射的注解信息,决定如何去运行类

23、自定义 Annotation

(1)定义新的 Annotation 类型使用 @interface 关键字

(2)声明注解的属性

注解属性的作用:原来写在配置文件中的信息,可以通过注解的属性进行描述。

Annotation 的属性声明方式:String name();

属性默认值声明方式:String name() default “xxx”;

特殊属性value:如果注解中有一个名称value的属性,那么使用注解时可以省略value=部分,如@MyAnnotation(“xxx")

特殊属性value[];

package com.itheima.annotation;

//都是Annotation子类

public @interface MyAnnotation1 {

String name();

int age();

String gender() default "male";

MyAnnotation2[] initParam();

}

package com.itheima.annotation;


public @interface MyAnnotation2 {

String paramName() default "";

String paramValue() default "";

}


package com.itheima.annotation;


public @interface MyAnnotation3 {

String value();

String name();

}

package com.itheima.annotation;


public @interface MyAnnotation4 {

String[] value();

}


package com.itheima.annotation;


public class MyAnnotationTest {

@MyAnnotation1(name = "shit", age = 18, gender = "female", initParam = {

@MyAnnotation2(paramName = "a", paramValue = "b"),

@MyAnnotation2(paramName = "x", paramValue = "y") })

//@MyAnnotation3(value="abc")

@MyAnnotation3(value="abc",name="xxx")//abc就是value属性赋值

@MyAnnotation4({"abc","def"})

public void m1() {


}

}

24、JDK 的元 Annotation

(1)元 Annotation指修饰Annotation的Annotation。JDK中定义了如下元Annotation:

(2)@Retention: 只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留的域, @Rentention 包含一个