5.在JAVA的程序中,我经常性的看到字符前面有@这种标志的符号.这个就叫做注解!
下面是使用 @SuppressWarnings 来取消 deprecation 警告的一个例子:
public class Test
{
@Deprecated //在eclipse下运行的时候,方法名上会加一横线
public static void sayHello()
{
}
}
public class Test2
{
@SuppressWarnings("deprecation") //在mian方法内调用一个没有定义的方法时,运行的时候将会出现这一注解!
public static void main(String [] args)
{
Test.sayHello();
}
}
@SuppressWarnings 批注允许您选择性地取消特定代码段(即,类或方法)中的警告。其中的想法是当您看到警告时,您将调查它,如果您确定它不是问题,您就可以添加一个 @SuppressWarnings 批注,以使您不会再看到警告。虽然它听起来似乎会屏蔽潜在的错误,但实际上它将提高代码安全性,因为它将防止您对警告无动于衷 — 您看到的每一个警告都将值得注意。
由如下代码引出@Override的讲解:
User类中的方法:
public boolean equals(User other)
{
return name.equals(other.name);
}
下面的代码执行时将有问题:
User user1 = new User();
User user2 = new User();
user1.setName("abc");
user2.setName("abc");
System.out.println(user1.equals(user2));
HashSet set = new HashSet();
set.add(user1);
set.add(user2);
System.out.println(set.size());//期望结果为1,但实际为2
这时候在User类的equals方法上加上@Override,发现了问题。
再看看这个代码:一看就知道有问题, 这里 就有个很好的解决办法,在写public @interface MyAnnotation {}这样的类的时候,下面的代码上的错误提示就是结束的!
public class dsds {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
System.out.println(User.class.isAnnotationPresent(MyAnnotation.class)
);
System.out.println(
User.class.getAnnotation(MyAnnotation.class)
);
}
}
运行的结果为:false
null
下面演示了一下@Target和@Retention
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE}) //用于构造方法
@Retention(RetentionPolicy.RUNTIME) //在运行是加载Annotation到JVM中
public @interface MyAnnotation {
public String value() default "yellow";
public int[] array() default {1,2};
public MetaAnnotation annotation() ;
}
注解最主要的就是这么多,其实注解真正的,我们都很少留心的,但是有的注解会给你在写程序和查找错误的时,会有很大的帮助的!
6.泛型:
1、泛型的类型参数只能是类类型(包括自定义类),不能是简单类型。
2、同一种泛型可以对应多个版本(因为参数类型是不确定的),不同版本的泛型类实例是不兼容的。
3、泛型的类型参数可以有多个。
4、泛型的参数类型可以使用extends语句,例如<T extends superclass>。习惯上成为“有界类型”。
5、泛型的参数类型还可以是通配符类型。例如Class<?> classType = Class.forName(java.lang.String);
例子:
a..使用?通配符可以引用其他各种参数化的类型,但不能调用与参数化有关的方法;
Collection<?> c = new Vector<String>();
c.add("abc");//报错
c.size();//正确
所以,?通配符定义的变量主要用作引用,调用与参数化无关的方法,如果要调用与参数化相关的方法,那么必须在使用?通配符引用之前调用,否则就与java 5提供泛型的目的背道而驰了。
b..向下限定通配符:
正确:Vector<? extends Number> x = new Vector<Integer>();
错误:Vector<? extends Number> x = new Vector<String>();
向上限定通配符:
正确:Vector<? super Integer> x = new Vector<Number>();
错误:Vector<? super Integer> x = new Vector<Byte>();
7.代理
这里就直接先应用张老师写的代码然后再讲清其原理吧!
package cn.itcast.corejava;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Vector;
public class ProxyTest {
public static void main(String[] args) {
System.out.println(Integer.class.getClassLoader());
//System.out.println(ProxyTest.class.getClassLoader().getParent().getClass().getName());
System.out.println(ProxyTest.class.getClassLoader().getClass().getName());
//test1();
//test2();
}
private static void test3() {
Vector v = new Vector();
class MyInvocationHandler implements InvocationHandler
{
Collection target = null;
public Collection bind(Collection target)
{
this.target = target;
Collection proxy1 = (Collection)Proxy.newProxyInstance(
ProxyTest.class.getClassLoader(),
new Class[]{Collection.class} ,
this);
return proxy1;
}
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("begin " + method.getName());
Object retval = method.invoke(target, args);
System.out.println("end" + method.getName());
return retval;
}
}
MyInvocationHandler handler = new MyInvocationHandler();
Collection proxy1 = handler.bind(v);
System.out.println(proxy1.getClass().getName());
proxy1.add("abc");
proxy1.add("xyz");
System.out.println(proxy1.size());
}
private static void test2() {
Vector v = new Vector();
class MyInvocationHandler implements InvocationHandler
{
Collection target = null;
public MyInvocationHandler(){}
public MyInvocationHandler(Collection target){this.target = target;}
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("begin " + method.getName());
Object retval = method.invoke(target, args);
System.out.println("end" + method.getName());
return retval;
}
}
Collection proxy1 = (Collection)Proxy.newProxyInstance(
ProxyTest.class.getClassLoader(),
new Class[]{Collection.class} ,
new MyInvocationHandler(v));
System.out.println(proxy1.getClass().getName());
proxy1.add("abc");
proxy1.add("xyz");
System.out.println(proxy1.size());
}
private static void test1() {
Collection proxy = (Collection)Proxy.newProxyInstance(
ProxyTest.class.getClassLoader(),//first parameter
new Class[]{Collection.class} , //second parameter
new InvocationHandler(){ //third parameter
Vector target = new Vector();
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("begin " + method.getName());
Object retval = method.invoke(target, args);
System.out.println("end" + method.getName());
return retval;
}
}
);
System.out.println(proxy.getClass().getName());
proxy.add("abc");
proxy.add("xyz");
System.out.println(proxy.size());
}
}
以下是Proxy的API文档:
-
public class Proxyextends
-
extends Object
-
implements Serializable
-
Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the
-
superclass of all dynamic proxy classes created by those methods.
-
To create a proxy for some interface Foo:
-
InvocationHandler handler = new MyInvocationHandler(...);
Class proxyClass = Proxy.getProxyClass(
Foo.class.getClassLoader(), new Class[] { Foo.class });
Foo f = (Foo) proxyClass.
getConstructor(new Class[] { InvocationHandler.class }).
newInstance(new Object[] { handler });
or more simply: -
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
从以上的程序中在方法中定义参数的时候总是三个参数,可这三个参数有分别代表什么意思呢!?现在以我自己个人的理解,比如我是买家,我要买本书,可是现在我有事,是不是需要别人代我买呢?那带我买的人是不是一定要到卖书的地方买呢?所以着中间产生了三个实体.说白了就是一种代理机制.需要三方一起运行! -
代理其实很好理解的,而且会用固定的语法格式,很快会掌握这一原理的!
从张老师的2天课的课程来看,涉及的内容还是很多的,很难,很重点的.但是一旦理解了,这些知识点在以后JAVA WEB开发中将受用不尽的!