黑马程序员---Java高新技术学习笔记-3

-------  android培训 java培训 、期待与您交流! ---------- 

1.注解

相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,之后java编译器开发工具和其他程序可以用反射的方式来了解你的类及各种元素上有无何种标记,看你有什么标记就去干相应的事,标记可以加在包,类,字段,方法方法的参数以及局部变量上。注解的属性:一个注解相当于一个胸牌,其中加入各种属性加以区分。
[java]  view plain copy print ?
  1. @ITAnnotation(color = "red",value = "abc",arrayAttr={1,2,3})//如果数组arrayAttr只有一个值的话  
  2.                             //可以简写成arrayAttr=1  
  3. @JavaEnhanceAnnotation(String={"abc","def","ghi","jkl"} , num = 5)  
  4. public class AnnotationTest {  
  5.     //注解的应用  
  6.     @SuppressWarnings("deprecation")  
  7.     //如果只有value属性需要设置,可以不写"value ="字段  
  8.     @ITAnnotation("uvwxyz")  
  9.     public static void main(String[] args) throws Exception  
  10.     {  
  11.         System.runFinalizersOnExit(true);//方法过时  
  12.         if(AnnotationTest.class.isAnnotationPresent(ITAnnotation.class))  
  13.         {  
  14.               
  15.             ITAnnotation annotationTest =   
  16.                     (ITAnnotation)AnnotationTest.class.getAnnotation(ITAnnotation.class);  
  17.             System.out.println(annotationTest.color());  
  18.             System.out.println(annotationTest.value());  
  19.             System.out.println(annotationTest.arrayAttr().length);  
  20.         }  
  21.         if(AnnotationTest.class.isAnnotationPresent(JavaEnhanceAnnotation.class))  
  22.         {  
  23.             JavaEnhanceAnnotation annotationTest =   
  24.                     (JavaEnhanceAnnotation)AnnotationTest.class.getAnnotation(JavaEnhanceAnnotation.class);  
  25.             System.out.println(annotationTest.String().length);  
  26.             System.out.println(annotationTest.num());  
  27.             System.out.println(annotationTest.lamp().nextLamp().name());  
  28.         }  
  29.     }  
  30.       
  31.     @Deprecated  
  32.     public static void sayHola()  
  33.     {  
  34.         System.out.println("lalala");  
  35.     }  
  36.       
  37. }  

元注解

根据放射测试的问题,引出@Retention(枚举类型)的三种取值:
1.RetentionPolicy.SOURCE-->java源文件
2.RetentionPolicy.CLASS-->class文件
3.RetentionPolicy.RUNTIME-->内存中的字节码

[java]  view plain copy print ?
  1. <span style="font-size:14px">import java.lang.annotation.*;  
  2.   
  3. @Retention(RetentionPolicy.RUNTIME)  
  4. @Target({ElementType.METHOD,ElementType.TYPE})  
  5. //Target表明注解只能放在方法以及类,接口或枚举类型的上方,别的地方无效  
  6. public @interface ITAnnotation   
  7. {     
  8.     //定义基本类型的属相和应用属性  
  9.     String color() default "blue";//设置ITAnnotation中的color为缺省属性  
  10.     String value();  
  11.     int[] arrayAttr() default {3,4,5};  
  12. }</span>  

2.泛型

它是提供给javac编译器使用的,可以限定集合中的输入类型,让编译器挡住源程序中的非法输入
编译器编译带类型说明的集合时会去除掉"类型信息",使程序运行效率不收影响,对于参数化的泛型类型
getClass()方法的返回值和原始类型完全一样,由于编译生成的字节码会去掉泛型的类型信息,只要能跳过
编译器,就可以通过反射的方法往某个泛型集合中加入其他类型的数据。

ArrayList<E>类定义和ArrayList<Integer>类引用中涉及如下术语:
1.整个称为ArrayList<E>泛型类型
2.ArrayList<E>中的E称为类型变量或者类型参数
3.整个ArrayList<Integer>称为参数化的类型
4.ArrayList<Integer>中的Integer称为类型参数的实例或实际类型参数
5.ArrayList<Integer>中的<>读typeof
6.ArrayList称为原始类型

参数化类型与原始类型的兼容性
1.参数化类型可以引用一个原始类型的对象,编译报告警告
Collection<String> c = new Vector();
2.原始类型可以引用一个参数化类型的对象,编译报告警告
Collection c = new Vector<String>();
3.参数化类型不考虑类型参数的继承关系
 1.Vector<String> v = new Vector<Object>();错误,不写<Object>不会出现错误
 2.Vector<Object> v = new Vector<String>();错误
 
4.在创建数组实例时,数组的元素不能使用参数化的类型
Vector<Integer> vectorList[] = new Vector<integer>[10];
 
Vector v1 = new Vector<String>();对
Vector<Object> v = v1;对

Vector<Object> v =new Vector<String>();错

[java]  view plain copy print ?
  1. import java.lang.reflect.Constructor;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.ParameterizedType;  
  4. import java.lang.reflect.Type;  
  5. import java.util.ArrayList;  
  6. import java.util.Collection;  
  7. import java.util.Date;  
  8. import java.util.HashMap;  
  9. import java.util.HashSet;  
  10. import java.util.Map;  
  11. import java.util.Set;  
  12. import java.util.Vector;  
  13.   
  14. import com.intel.day01.ReflectPoint;  
  15.   
  16. public class GenericTest   
  17. {  
  18.     public static void main(String[] args) throws Exception  
  19.     {  
  20.         ArrayList collection1 = new ArrayList();  
  21.         collection1.add(1);  
  22.         collection1.add(1L);  
  23.         collection1.add("abc");  
  24.         //int i = (Integer)collection1.get(1);  
  25.         //System.out.println(i);  
  26.           
  27.         /*================================华丽的分割线====================================*/  
  28.           
  29.         ArrayList<String> collection2 = new ArrayList<String>();  
  30.         collection2.add("abc");  
  31.         String element = collection2.get(0);  
  32.         System.out.println(element);  
  33.           
  34.         /*================================华丽的分割线====================================*/  
  35.           
  36.         //泛型的应用省去了类型转换之苦  
  37.         Constructor<String> cons = String.class.getConstructor(StringBuffer.class);  
  38.         String strr = cons.newInstance(new StringBuffer("abcd"));  
  39.         System.out.println("Generic:"+strr.charAt(2));  
  40.           
  41.         /*================================华丽的分割线====================================*/  
  42.           
  43.         ArrayList<Integer> collection3 = new ArrayList<Integer>();  
  44.         System.out.println(collection3.getClass() == collection2.getClass());  
  45.         //collection3.add("abcde");  
  46.         collection3.getClass().getMethod("add", Object.class).invoke(collection3, "abcde");  
  47.         System.out.println("Reflect:"+collection3.get(0));  
  48.         //此代码说明泛型的定义是给编译器看的,如果按普通的add添加方法,编译无法通过。  
  49.         //但是用反射的方法,就可以跳过编译器,忽略了泛型的字节码,便可以添加其他类型的参数。  
  50.           
  51.         /*================================华丽的分割线====================================*/  
  52.           
  53.         //泛型中的 ? 通配符的应用和扩展,详见方法  
  54.         printCollection(collection1);  
  55.           
  56.         /*================================华丽的分割线====================================*/  
  57.           
  58.         //泛型&迭代==》输出集合中的键和值  
  59.         HashMap<String,Integer> maps = new HashMap<String, Integer>();  
  60.         maps.put("zxx",28);  
  61.         maps.put("xxx",27);  
  62.         maps.put("zzz",26);  
  63.           
  64.         Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();  
  65.         for(Map.Entry<String,Integer> entry : entrySet)  
  66.         {  
  67.             System.out.println(entry.getKey() + ":" + entry.getValue());  
  68.         }  
  69.           
  70.         /*================================华丽的分割线====================================*/  
  71.           
  72.         //自定义反省类型及其应用  
  73.         add(1,2);  
  74.         add(2.3 , 5);  
  75.         add(3 , "abc");  
  76.         add('c' , 1L);  
  77.           
  78.         /*================================华丽的分割线====================================*/  
  79.           
  80.         swap(new String[]{"12","23","34"},1,2);  
  81.         //swap(new int[]{1,3,5,2,4},2,4);<T>不能为基本数据类型,必须是引用类型  
  82.           
  83.         /*================================华丽的分割线====================================*/  
  84.           
  85.         //DAO Data Access Object数据访问对象--->用于增删改查(C.R.U.D.)  
  86.         GenericDao<ReflectPoint> dao = new GenericDao<ReflectPoint>();  
  87.         dao.add(new ReflectPoint(33));  
  88.         //dao.findById(0);  
  89.         //dao.findByUserName("str");  
  90.         //dao.findByConditions("string");  
  91.           
  92.         /*================================华丽的分割线====================================*/  
  93.           
  94.         //通过反射获得泛型的实际参数类型  
  95.         //Vector<Date> v = new Vector<Date>();  
  96.         //System.out.println(v.getClass().getName());无法得到v的参数类型  
  97.         Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class);  
  98.         Type[] types = applyMethod.getGenericParameterTypes();  
  99.         ParameterizedType pType = (ParameterizedType)types[0];  
  100.         System.out.println(pType.getRawType().toString());//打印原始类型  
  101.         System.out.println(pType.getActualTypeArguments()[0]);  
  102.     }  
  103.       
  104.     private static <T> void swap(T[] a , int i , int j)  
  105.     {  
  106.         T tmp = a[i];  
  107.         a[i] = a[j];  
  108.         a[j] = tmp;  
  109.     }  
  110.   
  111.     public static void printCollection(Collection<?> collection)  
  112.     {  
  113.         //collection.add("1234");错误:因为他不知自己未来匹配的一定就是String类型  
  114.         //collection = new HashSet<Date>();//正确,因为用问号通配符定义的参数,可以指向任何类型的集合  
  115.         System.out.println("??:"+collection.size());//正确,此方法与参数类型无关  
  116.         for(Object col : collection)  
  117.         {  
  118.             System.out.println("?:"+col+"<>"+col.getClass().getName());  
  119.         }  
  120.           
  121.         /* 
  122.          * 限定通配符总是包括自己 
  123.         限定通配符的上界:通配符仅可以匹配Number和Number的子类 
  124.         Vector<? extends Number> x = new Vector<Integer>();//Y 
  125.         Vector<? extends Number> y = new Vector<String>();//N 
  126.         限定通配符的下界:通配符可以匹配的最低类型为integer,只能向上匹配 
  127.         Vector<? super Integer> x = new Vector<Number>();//Y 
  128.         Vector<? super Integer> x = new Vector<Byte>();//N虽然byte和integer是平级但是不行 
  129.         */  
  130.     }  
  131.       
  132.     private static <T> T add(T x , T y)  
  133.     {  
  134.         //return x + y;报错的原因是因为两个参数的类型不一定可以进行加法运算  
  135.         return null;  
  136.     }  
  137.       
  138.     public static void applyVector(Vector<Date> v)  
  139.     {  
  140.         System.out.println("通过反射获得泛型的实际参数类型");  
  141.     }  
  142. }  

3.类加载器

调用方法是需要用到的类,都需要通过类加载器加载进来
Java虚拟机中可以安装多个类加载器,系统默认有三个主要的类加载器,
每个类加载器负责加载特定位置的类:
BootStrap ExtClassLoader AppClassLoader

类加载器也是Java类,所以其本身也要被类加载器加载,显然必须有一个非java类的加载器BootStrap

JVM中的所以类加载器采用具有父子关系的的树形结构进行组织,在实例化每个类加载器对象时,需要
为其指定一个父级类加载器对象或者默认采用系统类加载器为其父级类加载
 
当Java虚拟机要加载一个类时,到底派哪个类去加载呢?
 1.首先,当前线程的类加载器去加载线程中的第一个类。
 2.如果类A中引用了类B。Java虚拟机将使用加载类A的类加载器来加载类B
 3.还可以直接调用ClassLoader.loadClass()方法来指定某个类加载器去加载某个类。

委托加载机制
每个类加载器加载类时,又先委托给其实上级类加载器,如果父级加载器已经将该类加载,则直接使用
避免了重复加载,占用内存资源
 1.当所有父级类加载器没有加载到类,回到发起者类加载器,还加载不了的话,则只能抛出
ClassNotFoundExceptipon,而不是再去找发起者类加载器的子级,因为没有getChild方法
即使有该方法,那么多儿子,也不知道该找哪一个
[java]  view plain copy print ?
  1. import java.util.Date;  
  2.   
  3. public class ClassLoaderTest extends ClassLoader  
  4. {  
  5.     public static void main(String[] args) throws Exception   
  6.     {  
  7.         System.out.println(ClassLoaderTest.class.getClassLoader().getClass().getName());  
  8.         //sun.misc.Launcher$AppClassLoader说明ClassLoader类是由AppClassLoader加载的  
  9.         System.out.println(System.class.getClassLoader());  
  10.         //null说明System类是由BootStrap加载的  
  11.           
  12.         ClassLoader loader = ClassLoaderTest.class.getClassLoader();  
  13.         while(loader!=null)  
  14.         {  
  15.             System.out.println(loader.getClass().getName());  
  16.             loader = loader.getParent();  
  17.         }  
  18.         System.out.println(loader+" --> $BootStrap");  
  19.         /* 
  20.         sun.misc.Launcher$AppClassLoader 
  21.         sun.misc.Launcher$ExtClassLoader 
  22.         null --> $BootStrap 
  23.         */  
  24.           
  25.         //System.out.println(new ClassLoaderAttachment().toString());  
  26.         /* 
  27.          * 打印结果-->Hola Maria... 
  28.          * 如果将intellib中的加过密的class文件吧bin中正常的class文件替换掉则会出现错误 
  29.              Exception in thread "main" java.lang.ClassFormatError: Incompatible magic  
  30.                value 889275713 in class file com/intel/day02/ClassLoaderAttachment 
  31.               出现此情况下,必须用自己定义的类加载器进行解密,前提需要继承ClassLoader类 
  32.               覆盖其findClass方法,详见MyClassLoader.java 
  33.          */  
  34.         //打印加密后的ClassLoaderAttachment.class文件  
  35.         Class clazz = new MyClassLoader("intellib").loadClass("ClassLoaderAttachment");  
  36.         Date d1 = (Date)clazz.newInstance();  
  37.         System.out.println(d1);  
  38.     }   
  39. }  
-------  android培训 java培训 、期待与您交流! ---------- 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值