js基础
1)对js的理解? 2)请说出以下代码输出的值? 3)把以下代码,改写成依次输出0-9 4)如何区分数组对象,普通对象,函数对象 5)面向对象、面向过程 6)面向对象的三大基本特性 7)XML和JSON的区别? 8)Web Worker 和webSocket? 9)Javascript垃圾回收方法? 10)new操作符具体干了什么呢? 11)js延迟加载的方式有哪些? 12)WEB应用从服务器主动推送Data到客户端有那些方式?
} }
特别注意:类型必须是基本类型、String、Class、注解类型、枚举及以上类型的一维数组。
### 二、注解的反射
##### **1、 AnnotationElement 接口**
JDK 5.0 在 java.lang.reflect 包下新增了 AnnotationElement 接口, 该接口代表程序中可以接受注释的程序元素,具有如下方法

* T getAnnotation(Class clazz):得到指定的注解类型
* Annotation[] getAnnotations():得到所有的注解类型
* Annotation[] getDeclaredAnnotations():得到自己上面的直接的注解类型
* boolean isAnnotationPresent(Class clazz):有没有指定的注解
**Class、Method、Field、Constructor等都实现了该接口。**
实例:编写一个自己的注解Anno1,在MyClass1中使用该注解,MyClass2继承MyClass1,这样,MyClass2也具有MyClass1上的注解,但是两者还是有差别的,看如下代码
package com.itheima.other.metaanno;
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD}) @Documented @Inherited public @interface Anno1 {
}
MyClass1
package com.itheima.other.metaanno; /** * 这是一个测试类 * @author wzhting * */ @Anno1 public class MyClass1 {
}
MyClass2
package com.itheima.other.metaanno;
import java.lang.annotation.Annotation;
public class MyClass2 extends MyClass1 { public static void main(String[] args) { Class clazz = MyClass2.class; // Anno1 a1 = (Anno1) clazz.getAnnotation(Anno1.class); // System.out.println(a1);
Annotation ans[] = clazz.getDeclaredAnnotations();//获取直接存在的注解
for(Annotation a:ans)
System.out.println(a);
}
}
比如:判断Object这个类上面有没有MyAnn1的注解
Class clazz = Object.class; Boolean b = clazz.isAnnotationPresent(MyAnn1.class);
##### **2、注解的生命周期:**
**2.1 AnnotatedElement元注解**
只能用在注解上的注解,就是元注解。JDK中定义了如下元Annotation:
* **`@Retention`**:
@Retention: 只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留的**域**, @Rentention 包含一个 RetentionPolicy 类型的成员变量, 通过这个变量指定域。
1. RetentionPolicy.CLASS: 编译器将把注解记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注解. 这是默认值
2. RetentionPolicy.RUNTIME:编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注解. 程序可以通过反射获取该注释
3. RetentionPolicy.SOURCE: 编译器直接丢弃这种策略的注释

* **`@Target`**:指示注解能用在何处
ElementType:指定注解用于修饰类的哪个成员. @Target 包含了一个名为 value,类型为ElementType的成员变量。看源码可知有`TYPE (Class,Interface), FIELD, METHOD, PARAMETER, ANNOTATION_TYPE`等声明常量。
**实例:自定义注解类MyTest**
首先定义两个实体类,用于单元测试分别为PersonDaoImpl 和测试类PersonDaoImplTest
PersonDaoImpl
package com.itheima.dao.impl;
public class PersonDaoImpl { public void add(){ System.out.println("保存了"); } public void del(){ System.out.println("执行了删除"); } }
PersonDaoImplTest
package com.itheima.dao.impl;
import org.junit.Test;
public class PersonDaoImplTest { private PersonDaoImpl dao = new PersonDaoImpl();
//测试方法:必须是public的;没有返回值;没有参数
@MyTest(timeout=1)//===>自己编写MyTest注解
public void testAdd() throws InterruptedException{
// Thread.sleep(100); dao.add(); } public void testDel(){ dao.del(); } }
自定义一个类MyTest,和JUnit的@Test类相似。
package com.itheima.dao.impl;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)//编译时,JVM保留注解 @Target(ElementType.METHOD)//使用于方法。
public @interface MyTest { long timeout() default -1;//没有时间限制。单位是毫秒 }
**反射注解的方法及属性(重点)**
通过反射技术,编写MyTestRunner 类,实现MyTest注解的功能。
package com.itheima.dao.impl;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
public class MyTestRunner {
public static void main(String[] args) throws Exception{
test1();
}
//反射注解的属性
private static void test2() throws IllegalAccessException,
InvocationTargetException, InstantiationException {
Class clazz = PersonDaoImplTest.class;
Method ms[] = clazz.getMethods();
for(Method m:ms){
MyTest myTest = m.getAnnotation(MyTest.class);//如果当前方法上面没有@MyTest注解,返回的是null
if(myTest!=null){
//执行该方法,判断是否超时
long time = System.currentTimeMillis();
m.invoke(clazz.newInstance(), null);
long actualUseTime = System.currentTimeMillis()-time;//实际耗时
//获取注解上配置的timeout属性的值
long planUserTime = myTest.timeout();
if(planUserTime>=0){
//说明用户指定了时间
if(actualUseTime>planUserTime)
throw new RuntimeException("超时");
}
}
}
}
//反射方法上面的注解
private static void test1() throws IllegalAccessException,
InvocationTargetException, InstantiationException {
//得到测试类的字节码
Class clazz = PersonDaoImplTest.class;
//得到所有的方法
Method ms[] = clazz.getMethods();
//判断方法上面有没有叫做@MyTest的注解
for(Method m:ms){
boolean b = m.isAnnotationPresent(MyTest.class);
System.out.println(m.getName()+":"+b);
//如果有:调用该方法
if(b)
m.invoke(clazz.newInstance(), null);
}
}
/* testAdd:true testDel:false wait:false wait:false wait:false equals:false toString:false hashCode:false getClass:false notify:false notifyAll:false*/
}
以下两个了解即可。
* **`@Documented`**:用于指定被该元 Annotation 修饰的 Annotation 类将被 javadoc 工具提取成文档.
* **`@Inherited`**:被它修饰的 Annotation 将具有继承性.如果某个类使用了被 @Inherited 修饰的 Annotation, 则其子类将自动具有该注解
### 三、servlet3.0 中的注解
在servlet3.0以后,就可以采用注解的方式取代web.xml文件里的servlet映射了
比如如下servlet
package com.itheima.servlet;
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration;
import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet(value = { "/servlet/ServletDemo1", "/servlet/ServletDemo11" }, initParams = { @WebInitParam(name = "encoding", value = "UTF-8"), @WebInitParam(name = "XXX", value = "YYY") }) public class ServletDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("ServletDemo1<hr/>");
ServletConfig cfg = getServletConfig();
Enumeration e = cfg.getInitParameterNames();
while(e.hasMoreElements()){
String paramName = (String)e.nextElement();
out.write(paramName+"="+cfg.getInitParameter(paramName)+"<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
js基础
1)对js的理解? 2)请说出以下代码输出的值? 3)把以下代码,改写成依次输出0-9 4)如何区分数组对象,普通对象,函数对象 5)面向对象、面向过程 6)面向对象的三大基本特性 7)XML和JSON的区别? 8)Web Worker 和webSocket? 9)Javascript垃圾回收方法? 10)new操作符具体干了什么呢? 11)js延迟加载的方式有哪些? 12)WEB应用从服务器主动推送Data到客户端有那些方式?