java基础复习篇(1) abstract,注解和动态代理

1. abstract


  abstract : 关键字 ,用于修饰类和方法
  1. 修饰的方法:没有方法体,只有方法的声明,这样的方法称为抽象方法。
  
  2.修饰的类:抽象类,不能被实列化。
 
  ps :
  1.有抽象方法的类一定是抽象类,但是抽象类中可以没有抽象方法。
  2.abstract不能修饰私有方法,构造方法,静态方法。

2.annotaion

1.自定义注解

//修饰Annotation的Annotation-JDK的元Annotation
@Target(value ={ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
//使用@interface来声明注解
public @interface HelloAnnotation {

	//使用接口中声明方法的方式来声明注解的属性
	String maior();
	int age ();
	
	//使用default 来设置默认值
	String school() default "school";
	
}

2.使用自定义注解

public class AnnotationTest {
	
	//抑制编译器警告注解
	@SuppressWarnings({ "rawtypes", "unused" })
	@Test
	public void helloAnnotation()
	{
		
		List list = new ArrayList();
	}

}
class A {
	@HelloAnnotation(age = 1,maior = "12")
	void test(){}
	void test2(@Deprecated String name)
	{
		
	}
	//用于表示某个程序(类,方法)已过时
	@Deprecated
	void test3(){}
	
}

class B extends A{
	
	@Override
	void test() {
		// TODO Auto-generated method stub
		super.test();
	}
	
}

3.动态代理

1.基于JDK的动态代理就需要知道两个类:
【1】InvocationHandler(接口)、
【2】Proxy(类)还要知道JDK是基于接口的动态代理
2.简单实列:
【1】需求:使用动态代理使用ArithmeticCalculator接口方法
【2】接口定义
ArithmeticCalculato接口

public interface ArithmeticCalculator {
	 
	 int add(int i,int j);
	 int sub(int i,int j);
	 int mul(int i,int j);
	 int div(int i,int j);
	 
}
	 ArithmeticCalculatorImpl类
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		// TODO Auto-generated method stub
		return i+j;
	}

	@Override
	public int sub(int i, int j) {
		// TODO Auto-generated method stub
		return i -j ;
	}

	@Override
	public int mul(int i, int j) {
		// TODO Auto-generated method stub
		return i*j;
	}

	@Override
	public int div(int i, int j) {
		// TODO Auto-generated method stub
		return i/j;
	}
}

动态代理:

	/*
	 * 关于动态代理的细节
	 * 1.需要一个被代理的对象
	 * 2.类加载器通常时和被代理对象使用相同的类加载器
	 * 3.一般地,Proxy.newProxyInstance()的返回值是一个被代理对象实现的接口的类型。
	 * 也可以是其他的接口类型。
	 * 注意:第二个参数,必须是一个接口类型的数组
	 * 提示:若代理对象不需要额外实现被代理对象实现的接口以外的接口,
	 * 可以使用target.getClass().getInterfaces()
	 * 4.InvocationHandler通常使匿名内部类的方式:被代理对象需要是一个final类型
	 * 5.InvocationHandler的invoke()方法的第一个参数Object proxy指的是
	 * 正在被返回的那个代理对象,一般情况下不适用他。
	 * 
	 * 
	 */
	final ArithmeticCalculator target = 
				new ArithmeticCalculatorImpl();
		Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(),
				target.getClass().getInterfaces(), 
				new InvocationHandler() {
					
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
						// TODO Auto-generated method stub
						return method.invoke(target, args);
					}
				});
		ArithmeticCalculator arithmeticCalculator =
				(ArithmeticCalculator) proxy;
    
	  System.out.println(arithmeticCalculator.add(10, 20));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值