什么是注解?
注解就是在java程序系统中,注解就是向编译器描述java程序之间的兼容的。下边就通过例子来看看实实在在的注解:
例子:package com.heima;
public class Annotation {
public static void main(String[] args) {
@SuppressWarnings("deprecation");
System.runFinalizersOnExit(true);
Annotation.print();
}
@Deprecated
public static void print(){
System.out.print("Hellowrld");
}
}
还有一种java本身的注解就是Override是让JVM检验方法是从父类中的覆盖还是重载。
进一步了解注解:
注解的使用流程是这样的:定义注解——让一个类使用注解——让一个反射类反射这个注解的类。下边是个例子:
定义注解:package com.heima;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)//向编译器打招呼说明这个注解是在运行阶段。
@Target(ElementType.METHOD);//也可以这样({ElementType.METHOD,ElementType.TYPE})
public @interface AnnotationTest {
String decration()default "east";//设置的缺省值。
int value();//如果注解中只有这一个属性,可以这样写AnnotationTest(2)
int []Arrays() default {3,3,3};//【】不能放在数组名的后边;调用
DecrationEnum enm()default DecrationEnum.next("东");//枚举类型的属性
AnnotTest test()default @AnnotTest(2);//注解类型的属性
}
使用注解的类以及反射:
package com.heima;
@AnnotationTest(3)
public class Annotation {
public static void main(String[] args) {
if( Annotation.class.isAnnotationPresent(AnnotationTest.class)){
AnnotationTest test=Annotation.class .getAnnotation(AnnotationTest.class);
System.out.println(test);
}
System.runFinalizersOnExit(true);
Annotation.print();
}
@AnnotationTest(value=1,Arrays={2,3,4,4})//如果你的值只有一个可以这么写Arrays=3;
public static void print(){
System.out.print("Hellowrld");
}
}
java1.5中的泛型应用:
首先通过一个例子来认识泛型:
package com.heima
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Generics {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("name","姓名");
map.put("age","年龄");
map.put("dress","地址");
Set<Map.Entry<String,String>>set= map.entrySet();
for(Map.Entry<String, String>entry:set){
System.out.println(entry.getKey()+" : "+entry.getValue());
}
Set<String> set1= map.keySet();
for(String str:set1){
System.out.println(str+"---->"+ map.get(str));
}
}
}
泛型的使用可以为我们提高代码的安全,泛型作用在编译阶段的,在运行时没没有限定了。所以,我们用反射来操作结合类的话泛型是不起作用的。
注意:java中还扩展符?和extends限定符
下边就进入了怎么去定义自己的泛型方法:
还是一段代码:
package com.heima;
public class GenericsTest {
public static void main(String[] args) {
String []arrays=new String[]{"agb","234","abc"};
Integer igr[]=new Integer[]{2,3,4};
GenericsTest.ergod(igr);
GenericsTest.ergod(arrays);
}
public static <T>void ergod(T arrays[]){
for(T t:arrays){
System.out.println(t);
}
}
}
注意:给ergod方法传进来的数组不能是基本数据类型的数组,比如:int float等。
java中的代理;
代理就是比如我们定义好了一个类,但是我们想给这个类中的方法增加其他的功能,而我们有不想修改类的本身,这是我们就可以这个类动态的则加一个代理类就行了,下边通过代码来看看:
例子:
package com.heima;
import java.awt.List;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
public class CopyPictuer {
public static void main(String[] args) {
ArrayList list=new ArrayList();
list.add(2);
list.add(3);
list.add(4);
getproxy(list,new Action());
list.size();
}
public static void getproxy(final Object goj,final Action acioin){
Proxy.newProxyInstance(goj.getClass().getClassLoader(),
goj.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
acioin.beforruntime(arg1);
Object relvalue=arg1.invoke(goj, arg2);
acioin.endruntime(arg1);
// TODO Auto-generated method stub
return relvalue;
}
}
);
}
}
interface Actiontest{
void beforruntime(Method method);
void endruntime(Method method);
}
class Action implements Actiontest{
long starttime;
public void beforruntime(Method method) {
// TODO Auto-generated method stub
starttime=System.currentTimeMillis();
}
public void endruntime(Method method) {
// TODO Auto-generated method stub
long endtime=System.currentTimeMillis();
System.out.println(method.getName()+"the program si runing time:" + (endtime-starttime));
}
}