黑马程序员_高新技术学习笔记(第六、七章)
来源:互联网 发布:网络拷克 编辑:程序博客网 时间:2024/05/24 01:46
---------------------- android培训、java培训、期待与您交流! ----------------------
注解Annotation(重要)
Annotation+Test
编码习惯
类名属性名变量名
名词
形容词+名词
方法
动词
动词+名词
ssh都使用了注解
告诉javac编译器
@SupperessWarnings("deprecation")
相当于创建了一个类的实例对象
@Deprecated
@Override
equals(ReflectPoint rp)
equals(Object obj)
反射判断有无某种标记
@interface A{}
@A//可以添加多个注解
Class B{}
Class C{
B.class.isAnnotationPresent(A.class);
A a = (A)B.class.getAnnotation(A.class);
}
注解的注解
元注解:
java.lang.annotation.Retention;
java.lang.annotation.RetentionPlolicy;
@Retention(RetentionPolicy.RUNTIEM) //枚举 保留到运行期间
javac编译成.class文件
注解生命周期
源文件 //
class文件 //默认值保留到class阶段 @Overr@SuppressWarnings
内存中字节码 //RUNTIME
@Deprecated RUNTIME //System.runFinalizersOnExit(true); 编译器用的是System的内存中的二进制
@Target(ElementType.METHOD)//定义注解的位置
多个ElementType.TYPE//Class(包括interface Enum @interface)实现了Type 接口 用Type描述准确
注解增加属性(很像方法)
String color();//缺省属性值 String color() default "blue";
设置值
@ItcastAnnotation(color="RED",value="",attayAttr={1,2,3})attayAttr=1 省略{}
用
annotation.color()
String value();
注解中添加数组类型的属性
int[] arrayAttr() default {3,4,4}
annotation.arrayAttr().size()//整数数组无法打印
int[] 不会当成Object[]
注解添加枚举类型的属性
EnumTest.Traffficlamp lamp() default EnumTest.Traffficlamp.RED;
annotation.lamp().nextLamp().name();
添加注解类型的属性
MetaAnnotation annotationAttr() default @MetaAnnotation("lhm")
@ItcastAnnotation(annotationAttr=@MeatAnnotation("flx"))
annotation.annotationAttr().value()
Class类型
所有上述类型的数组
泛型Generic
变量重构
raw原始
collection2.getClass()==collection3.getClass();
泛型是给编译器看的
Collection<Integer> collection3 = new ArrayList<Integer>();
collection3.getClass().getMethod("add",Object obj).invoke(collection3,"abc");
collection3,get(0)
ArrayList<Integer> ArrayList typeof Integer
Collection <String> c = new Vector();//相反,警告
泛型参数化类型不考虑继承
Vector<String> v = new Vector<Object>();
数组的元素不能使用参数化的类型
Vector v1 = new Vector<String>();
Vector<Object> v=v1;
泛型的通配符应用
定义一个方法打印任意参数化类型的集合中的所有数据
public static void printCollection(Collection<?> collection){
collection.add();错//不能调用
collection.size();对//方法与类型无关
for(Object obj:collection){
System.out.println(obj);
}
}
Collection<?> col
col=new HashSet<Date>();//可以
使用?参数化类型通常作为引用,可以调用与参数化无关的内容
通配符的扩展
extends Number 上边界
super Integer 下边界
Vector<? extends Number> x = new Vector<Integer>();//Integer改成String不行,错误
Class<String> x = /Class<?>/Class.forName("java.lang.String");
泛型综合实例
Map.Entry
Interface Map 没有实现Iterable接口,所以不能迭代
只能得到一个set集合set 可以迭代 Iterable
Set<Map.Entry<String,Integer>> entrySet =maps.entrySet();
for(Map.Entry<String,Integer> entry:entrySet)
{
System.out.println("entry.getKey()+":"+"entry.getValue()");
}
entry javabean key value 迭代用得多
C++模板
T add(T x,T y){
return(T)(x+y);
}
jvm的指令集改造,java虚拟机厂商
private static <T> T add(T x,T y){
}
add(3,5);
add(3.5,3);
add(3,"abc");
类型推断
交换数组中两个元素的位置
private static void swap(int[] a,int i,intj){
}
private static <T> void swap(T[] a,int i,int j){
T tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
只有引用类型才能作为泛型方法的实际参数
swap(new String[]{"www.","baidu",".com"},1,2); 对
swap(new int[]{1,2,3,5},2,3); 错//×××add(2,3)
public <A extends Annotation&***>A getAnnotation(Class<A> annotationClass)
泛型异常???
不能用于catch
private static <T extends Exception>sayHello() throws T
{
try{}
catch(Exception e){
throw (T)e;
}
}
自动将Object类型转换成其它类型
public static <T> T autoConvert(Object obj){
return (T)obj;
}
Object obj="abc";
String x=autoConvert(obj);
将任意类型的数组填充为相应类型的某个对象
fillArray(T[] a,T obj)
{
for(int i=0;i<a.length;i++)
{
a[i]=obj;
}
}
采用自定义泛型方法的方式打印出任意参数化类型的集合中所有的内容
public static void printCollection(Collection<?> collection){
collection.add();错//不能调用——————更有效
public static <T> void printCollection(Collection<T> collection,T obj2){
collection.add(obj2);对
把任意参数类型的一个//集合//数组//中的数据安全地复制到相应类型的另一个数组中
两者有区别
static <T> void copy1(collection<T> dest,T[] src)
static <T> void copy2(T[] dest,T[] src)
copy1(new Vector<String>(),new String[10]);
copy2(new Date[10],new String[10]); 公约数 Object
copy1(new Vector<Date>(),new String[10]);错 类型推断传播性
类型参数的类型推断
无返回值 最大交集类型
有返回值,优先考虑返回值
类型推断具有传递性???
在类上定义泛型
crud
dao
database access object
public class GenericDao<T>{
public void add(T x){
}
public void T findById(int id){
}
public void delete(T obj){
}
public void delete(int id){
}
public void update(T obj){
}
public T findByUserName(String name){
}
public Set<T> findByConditions(String where){
return null;
}
GenericDao<ReflectPoint> dao = new GenericDao<ReflectPoint>();
dao.add(new ReflectPoint(3,3));
//String str = dao.fingById(001);
ReflectPoint rp = dao.findById(007);
static 方法 泛型 独立
方法签名?
Vector<Date> v1 = new Vector<Date>();
v1.getClass()
public static void applyVector(Vector<Date> v1)
不能通过v1前面变量的类型
能通过这个方法知道他的参数里表的类型
java.lang.reflect.Method;
Method类 getGenericReturnType/getGenericParameterTypes/getParameterTypes
Method applyMethod = GenericTest.class.getMethod("applyVector",Vector.class)
Type[] types = appayMethod.getGenericParameterTypes();
Class只是Type的一个//ParameterizedType pType = (ParameterizedType)types[0];
pType.getRawType();//得到原始类型______Vector;
pType.getActualTypeArguments()[0]______Date;
框架经常使用的技术
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima
- 黑马程序员_高新技术学习笔记(第六、七章)
- 黑马程序员_高新技术学习笔记(第五章_上)
- 黑马程序员_高新技术学习笔记(第五章_下)
- 黑马程序员_高新技术学习笔记(第三、四章)
- 黑马程序员_高新技术学习笔记第八章
- 黑马程序员_高新技术学习笔记第九章
- 黑马程序员_高新技术笔记
- 黑马程序员_对于Java高新技术的学习笔记
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- 黑马程序员_高新技术
- php 字符串操作 字符 php
- Activity回传数据
- USACO Fractions to Decimals
- Linux Socket网络编程(理论分析+充足的示例)
- 两种SVN开发模式实现目录结构布局
- 黑马程序员_高新技术学习笔记(第六、七章)
- showModalDialog相关问题
- 第一个Flex 的程序出炉了
- linux经典书籍(转)
- 解压时出现WinRAR诊断信息(无法创建文件,文件名过长),怎么办!!!
- 常见SVN用法详解
- IT公司智力题(持续跟新中)
- 该严惩讹诈好人的恐怖老太吗?
- DevSuite及DevTest安装注意事项